当前位置:网站首页>Self taught C special data type

Self taught C special data type

2022-06-24 18:39:00 MousseIn

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
原网站

版权声明
本文为[MousseIn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211332353950.html