Parameters and special types and features in


1、 Optional and named parameters

1. Optional parameters


[modifier] return type method name (required parameter 1… Required parameter n, optional parameter 1… Optional parameter n)


static void BookList(string category,int pageIndex = 1)
        static void Main(string[] args)
          BookList("C#"); // Use all default parameters
          Booklist ("C #", 2) // do not use default parameters

2. Named parameters


                Method name (parameter 1 Name: parameter 1 value… Parameter n Name: parameter n value)                 


static void BookList(string category,int pageIndex = 1)
        static void Main(string[] args)
          BookList(category:"C#"); // Use all default parameters
          Booklist (category: "C #", PageIndex: 2) // do not use default parameters

2、 Special types of. Net

      1. Implicit type

Implicit type has the following applications: declaring local type variable, for initialization statement, foreach initialization statement, using statement


var list = new Student(); // Student is a custom type

Note: when VaR is used, the value must be assigned while the variable is declared

     2. Anonymous type (anonymous type can be created by new operator and initial value)

            New {attribute 1 Name: attribute 1 value,… Attribute n Name: attribute n value}             


Var stu = new {name = Zhang San, age = 18, gender = male};

            Note: the assignment of an attribute in an anonymous type is one-time, that is, the attribute of an anonymous pair is read-only            

    3. Dynamic type (define dynamic type) 

//Create a dynamic type object
       dynamic person1 = new Student{Name="Bing",Age=20};

            No error will be reported at compile time and at run time, because there is no introduce method             

   4. What is the difference between dynamic and VaR keywords?

Var can only be used for local variables, not for fields and parameters; Declaration must be initialized at the same time; The type of the variable is determined at compile time

Dynmic is used for type fields, method parameters, method return values, and generic type parameters; Any type can be assigned or assigned

                    No cast is required

        5. Nullable type

         1. Grammar:  

System. Nullable < type > variable name


                    Type? Variable name                     


System.Nullable<int> num = null;
             System.Nullable<DateTime> birthday = null;
             int? num = null;
            DateTime? birthday = null;

Note: when assigning a nullable type to a non nullable type, a compilation error will be raised

   eg:int? num = null; int num2 = num;

         2. The property of nullable type can be used to solve the problem that a nullable type cannot be assigned to a non nullable type

(1) Hasvalue: of bool type. It is set to true when the variable contains a non null value

                ( 2) Value: if hasvalue is true, it indicates that the value contains a meaningful value; otherwise, an invalidoperationexception will be thrown                           

   int? num1 = 5
        int num2 = num1??0;    

3、 Characteristics

      1. The main characteristics of C # are as follows

Add additional information to the target element (which can be assembly, class, attribute, method), similar to comments

Feature is also a ray in nature, directly or indirectly inherited from the attribute class

Attribute names all end with attribute, but can be omitted when using it. Net will automatically find the corresponding attribute class

    2. Grammar

            [ Property name] or [property name (parameter value…)]             


[object] // this method can be used, but it will be warned at compile time
        [obsolete ("don't use the old method, use the new method", false)] // this method can be used, but it will be warned at compile time
        [obsolete ("don't use the old method, use the new method", true)] // this method can't be used, and an error will be reported during compilation
        static void Old(){
          Console. Writeline ("this is the old method!");
        static void New(){
          Console. Writeline ("this is a new method!");
        public static void Main(){

      3. Custom attribute (inherit attribute)


      [attributeusage (attributeusages. Class)] // can only be used in classes
      [attributeusage (attributeusage. Method)] // can only be used in methods
      [attributeusage (AllowMultiple = true)] // can be used multiple times on the same class
      class DescriptionAttribute:Attribute{
        public string name{get;set;}
        public DescriptionAttribute(){}
        public DescriptionAttribute(string name){
 = name

The above is the parameters and special types and features of introduced by Xiaobian. I hope it can help you. If you have any questions, please leave me a message and Xiaobian will reply you in time. Thank you very much for your support to developer!

Recommended Today

Programming Xiaobai must understand the network principle

How is the network composed? Why can we easily surf the Internet now?Whether you are a computer major or not, you may always have such questions in your heart!And today we will solve this matter and tell you the real answer! Basic composition of network First, let’s look at this sentence Connect all computers together […]