当前位置:网站首页>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
边栏推荐
- 面试算法 - 字符串问题总结
- Creating a new MySQL user in Amazon RDS environment - creating a new MySQL user in Amazon RDS environment
- He "painted" what a smart city should look like with his oars
- [quick news] the jeecgboot low code platform was successfully selected into the 2021 scientific innovation China · open source innovation list
- Ultimate Guide: comprehensive analysis of log analysis architecture of Enterprise Cloud native PAAS platform
- Redis learning -- list of redis operations
- Sword finger offer 10- ii Frog jumping on steps
- Regression testing strategy for comprehensive quality assurance system
- Five advantages and disadvantages of Bi
- Vite+web3:报错出现ReferenceError: process is not defined
猜你喜欢

How MySQL works - Chapter 14

Flutter dart regular regexp matches non printing characters \cl\cj\cm\ck

Get the actual name of the method parameter through the parameter

It is often blocked by R & D and operation? You need to master the 8 steps before realizing the requirements

Network security database penetration of secondary vocational group in 2022
Business based precipitation component = & gt; manage-table

High quality defect analysis: let yourself write fewer bugs

Software testing methods: a short guide to quality assurance (QA) models

Regression testing strategy for comprehensive quality assurance system
Ultimate Guide: comprehensive analysis of log analysis architecture of Enterprise Cloud native PAAS platform
随机推荐
The mixed calculation of rpx and PX in JS by the uniapp applet
congratulate! The first dragon lizard community annual outstanding contribution award is announced. Check it now
[golang] leetcode intermediate - jumping game & different paths
What is decision intelligence?
如何在 R 中使用 Fisher 的最小显着性差异 (LSD)
Software testing methods: a short guide to quality assurance (QA) models
Graph traversal (BFS and DFS) C language pure handwriting
How do yaml files and zmail collide with the spark of the framework, and how can code and data be separated gracefully?
Selection (031) -cool_ How long can secret be accessed?
How to perform power regression in R
How to select the best test cases for automation?
JS picture display and hiding cases
How to use Fisher's least significant difference (LSD) in R
Flex box flex attribute
Uniapp wechat applet calls mobile map to navigate to the target point
Industry Cloud video editing software
Why should state-owned enterprises accelerate the digital transformation
[quick news] the jeecgboot low code platform was successfully selected into the 2021 scientific innovation China · open source innovation list
Huitongda officially landed at the Hong Kong Stock Exchange: the gross profit margin continued to decline, and the book value of several shareholders still suffered losses
Microservice system design -- interface document management design