当前位置:网站首页>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
边栏推荐
- Microservice system design -- data model and system architecture design
- Mental models: the best way to make informed decisions - farnam
- How to perform robust regression in R
- Online sequence flow chart making tool
- Selection (030) - what is the output of the following code?
- JS deep understanding of functions
- 130. surrounding area
- Flutter dart regular regexp matches non printing characters \cl\cj\cm\ck
- Recommend a distributed JVM monitoring tool, which is very practical!
- Does the wave of layoffs in Chinese enterprises in 2021 need to be "judged" by morality?
猜你喜欢
History object
How do yaml files and zmail collide with the spark of the framework, and how can code and data be separated gracefully?
Location object
JS position operation
SAP license: ERP for supply chain management and Implementation
[quick news] the jeecgboot low code platform was successfully selected into the 2021 scientific innovation China · open source innovation list
Overall planning and construction method of digital transformation
电源效率测试
JS string method
DOM (document object model)
随机推荐
How to perform robust regression in R
Palindrome string (two methods)
How to perform power regression in R
Flutter dart regular regexp matches non printing characters \cl\cj\cm\ck
Regression testing strategy for comprehensive quality assurance system
How to create a linear model prediction interval in R and visualize it
Recommend 14 commonly used test development tools
Interview algorithm - string question summary
Solve the problem that the MapReduce program console does not have log information warn please initialize the log4j system properly
Paper sharing | self supervised learning paper jointly released by Yann Lecun and read by engineers
面试算法 - 字符串问题总结
Restcloud ETL extracting dynamic library table data
Redis series (3) - sentry highly available
Is it safe to open an account online? What should I do?
Vite+web3:报错出现ReferenceError: process is not defined
Business based precipitation component = & gt; manage-table
congratulate! The first dragon lizard community annual outstanding contribution award is announced. Check it now
DOM (document object model)
Rapidssl getting started SSL certificate
Does the wave of layoffs in Chinese enterprises in 2021 need to be "judged" by morality?