当前位置:网站首页>Self taught C special data type
Self taught C special data type
2022-06-24 18:39:00 【MousseIn】
C# Special data types
Use optional and default parameters
Optional parameters are also called default parameters
[ Modifier ] Return type Method name ( Parameters 1… Parameters n, Optional parameters 1… Optional parameters n)
among , The required parameter must precede the optional parameter , And the parameter must be given when the method is called , Otherwise, compilation error will occur .
#region bage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using static System.Convert;
#endregion
namespace Test
{
class Program
{
public static void add( int num1,int num2 = 10)
{
WriteLine(num1+num2);
}
static void Main(string[] args)
{
Program.add(30,50);
ReadKey();
}
}
}
Named parameters
Specify the parameter name when calling the method
The grammar is as follows :
Method name ( Parameter 1 : Parameter one value … Parameters n name : Parameters n value )
Examples are as follows :
#region bage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using static System.Convert;
#endregion
namespace Test
{
class Program
{
public static void add( int num1,int num2)
{
WriteLine(num1);
WriteLine(num2);
}
static void Main(string[] args)
{
Program.add(num2:30,num1:50);
ReadKey();
}
}
}
Understanding implicit types 、 Anonymous types and dynamic type
Implicit type
- The compiler infers types
var Variable name = A variable's value
Must be assigned at the same time when declaring , Equivalent to after assignment var Just know what type you represent .
Examples are as follows :
#region page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#endregion
namespace Test
{
class Program
{
static void Main(string[] args)
{
var i = "string";
Console.WriteLine(i);
Console.ReadLine();
}
public static void add(int num1,int num2)
{
Console.WriteLine(num1);
Console.WriteLine(num2);
}
}
}
Anonymous types
grammar :
new{ attribute 1 name : attribute 1 value ,… attribute n name : attribute n value }
There is no need to specify the attribute type
Examples are as follows :
namespace Test
{
class Program
{
static void Main(string[] args)
{
//var i = "string";
//Console.WriteLine(i);
var tmp = new {
id = 123,name =" Zhang San "};
// Except for the type name and the value that cannot be modified , Other operation methods are the same as those of classes
Console.WriteLine(tmp.id);
Console.WriteLine(tmp.name);
// Generally used in data transmission and routing
Console.ReadLine();
}
}
}
Anonymous types are commonly used in data transfer and routing
The value cannot be modified except that there is no type name , Other operation methods are the same as those of the class
The given parameter values are the same as constants , You cannot change .
dynamic type
- Do type checking at runtime ( Dynamic type )
- Can be used for variable types , Method parameters and return value types
Examples are as follows :
#region page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#endregion
namespace Test
{
class Program
{
static void Main(string[] args)
{
//var i = "string";
//Console.WriteLine(i);
// var tmp = new { id = 123,name =" Zhang San "};
// Except for the type name and the value that cannot be modified , Other operation methods are the same as those of classes
//Console.WriteLine(tmp.id);
//Console.WriteLine(tmp.name);
// Generally used in data transmission and routing
dynamic d = "string";
Console.WriteLine(d);
Console.WriteLine(d + 30);
d = 123;
Console.WriteLine(d);
Console.WriteLine(d+30);
// It is the runtime that determines
object obj = 123;
Console.WriteLine(Convert.ToInt32(obj) +30);
Console.ReadLine();
}
}
}
Dynamic type , Determine the type at run time , No matter what the initial assignment is, no compilation error will be reported , It can also store any type of object Type does not require explicit or implicit conversion of data types .
It's easy to use , But every time the program calls a defined dynamic type, it will do a type verification for this parameter , It will increase the performance burden of the system . Use only when you don't know what type of parameter it should be .
Master the usage of nullable types
Null type
- Nullable type allows the value type to be set to null (null)
- For numeric types
The grammar is as follows :
System.Nullable< type > Variable name
type ? Variable name
Examples are as follows :
#region page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#endregion
namespace Test
{
class Program
{
//public static void add(int num1,int num2)
//{
// Console.WriteLine(num1);
// Console.WriteLine(num2);
//}
static void Main(string[] args)
{
//var i = "string";
//Console.WriteLine(i);
// var tmp = new { id = 123,name =" Zhang San "};
// Except for the type name and the value that cannot be modified , Other operation methods are the same as those of classes
//Console.WriteLine(tmp.id);
//Console.WriteLine(tmp.name);
// Generally used in data transmission and routing
//dynamic d = "string";
//Console.WriteLine(d);
//Console.WriteLine(d + 30);
//d = 123;
//Console.WriteLine(d);
//Console.WriteLine(d+30);
// It is the runtime that determines
int ? i = 9 ;
int j = (int)i;
Console.WriteLine(i+j);
Console.ReadLine();
}
}
}
One ? The representation is to change the variable represented by the variable name after the question mark into an nullable type , And two ?? Indicates that a default value is added .
Examples are as follows :
#region page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#endregion
namespace Test
{
class Program
{
//public static void add(int num1,int num2)
//{
// Console.WriteLine(num1);
// Console.WriteLine(num2);
//}
static void Main(string[] args)
{
//var i = "string";
//Console.WriteLine(i);
// var tmp = new { id = 123,name =" Zhang San "};
// Except for the type name and the value that cannot be modified , Other operation methods are the same as those of classes
//Console.WriteLine(tmp.id);
//Console.WriteLine(tmp.name);
// Generally used in data transmission and routing
//dynamic d = "string";
//Console.WriteLine(d);
//Console.WriteLine(d + 30);
//d = 123;
//Console.WriteLine(d);
//Console.WriteLine(d+30);
// It is the runtime that determines
int ? i = null ;
//int j = i.HasValue ? i.Value : 0;// I made a judgment with the ternary operator ,i If it's not worth it , will 0 Assign a value to i;
int j = i ?? 9;// Two question marks are equivalent to getting the default value
Console.WriteLine(j);
string str = null;
string str2 = str ?? "10";
Console.ReadLine();
}
}
}
Understand the usage of features
- similar JAVA The annotations in
- Inherited from Attribute Special types of
- Can be used in methods , Classes and assemblies
边栏推荐
- Restful design method
- Digital trend analysis of B2B e-commerce market mode and trading capacity in electronic components industry
- How does the video platform import the old database into the new database?
- How MySQL works - Chapter 14
- JS string method
- Eight digit
- Wechat applet development - Implementation of rotation chart
- 如何在 R 中创建线性模型预测区间 并可视化
- An analysis of the comments on the TV series Douban by procedural apes
- Software testing methods: a short guide to quality assurance (QA) models
猜你喜欢
[quick news] the jeecgboot low code platform was successfully selected into the 2021 scientific innovation China · open source innovation list

【leetcode】838. Push domino (Analog)

Crmeb multi merchant PC packaging tutorial

Nine practical guidelines for improving responsive design testing

Sudoku (easy to understand)

696. count binary substring
![[untitled]](/img/ab/066923f1aa1e8dd8dcc572cb60a25d.jpg)
[untitled]

Skills of writing test cases efficiently
![[NLP] 3 papers on how Stanford team builds a better chat AI](/img/f1/1c2ff31a728152395618800600df45.jpg)
[NLP] 3 papers on how Stanford team builds a better chat AI

It is often blocked by R & D and operation? You need to master the 8 steps before realizing the requirements
随机推荐
DOM (document object model)
Gateway solves cross domain access
股票网上开户安全吗?应该怎么办理?
Is it safe to open an account online? What should I do?
[golang] leetcode intermediate - jumping game & different paths
Project Management Guide: tips, strategies and specific practices
Navigator object
Vite+web3:报错出现ReferenceError: process is not defined
Rapidssl getting started SSL certificate
Nacos cluster starts throwing set of SQL_ SELECT_ LIMIT is not support
Mcu-08 interrupt system and external interrupt application
如何在 R 中执行稳健回归
SAP license:sap s/4hana is the answer
R中的指数回归
如何在 R 中执行幂回归
An analysis of the comments on the TV series Douban by procedural apes
How does the video platform import the old database into the new database?
Leetcode topic [array] -216- combined sum III
Digital transformation informatization data planning and technology planning
Selection (033) - what is the output of the following code?