Parameters and special types and properties in ASP. NET

  • 2021-10-11 18:03:10
  • OfStack

1. Optional and named parameters

1. Optional parameters

Syntax:

[Modifier] Return type method name (required parameter 1... required parameter n, optional parameter 1... optional parameter n)

eg:


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

2. Named parameters

Syntax:

Method name (parameter 1 name: parameter 1 value... parameter n name: parameter n value)

eg:


static void BookList(string category,int pageIndex = 1)
        {
          // Operation 
        }
        // Call 
        static void Main(string[] args)
        {
          BookList(category:"C#"); // Use all default parameters 
          BookList(category:"C#" , pageIndex:2) // Do not use default parameters 
        }

2.. Special type of NET

1. Implicit types

Implicit types are mainly used in the following situations: declaring local type variables, for initialization statements, foreach initialization statements, and using statements

eg:


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

Note: With var, you must assign values at the same time as declaring variables

2. Anonymous types (anonymous types can be created with the new operator and alignment of initial values)

new {Attribute 1 Name: Attribute 1 Value,... Attribute n Name: Attribute n Value}

eg:


   var stu = new {Name=" Zhang 3",Age=18,Gender=" Male "};       

Note: The assignment of attributes in anonymous types is quadratic, that is, the attributes of anonymous pairs are read-only

3. dynamic type (define dynamic type)


 // Create 1 Dynamic type objects 
       dynamic person1 = new Student{Name="Bing",Age=20};
      person1.Introduce();

Errors are not reported at compile time, but 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 fields and parameters; The declaration must be initialized at the same time; The type of the variable is determined at compile time

dynmic for type fields, method parameters, method return values, can be used for generic type parameters, and so on; You can assign or be assigned any type

No forced type conversion is required

5. Nullable types

1. Syntax:

System.Nullable < Type > Variable name

Or

Type? Variable name

eg:


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

Note: When assigning a nullable type to a non-nullable type, a compilation error will be thrown


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

2. You can use the attribute of nullable type to solve the problem that you can't assign a nullable type to a non-nullable type

(1) HasValue: Is of type bool and is set to true when the variable contains a non-null value

(2) Value: If HasValue is true, Value contains a meaningful value, otherwise InvalidOperaionException is raised


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

3. Characteristics

1. C # features the following

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

The property is also essentially a mine, directly or indirectly inherited from the Acttribute class

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

2. Grammar

Attribute name or attribute name (parameter value...)

eg:


[Obsolete]  // This method can be used using the   But compile time warns 
        [Obsolete(" Don't use the old method, use the new method ",false)]  // This method can be used using the   But compile time warns 
        [Obsolete(" Don't use the old method, use the new method ",true)]   // This method cannot be used, and an error will be reported at compile time 
        static void Old(){
          Console.WriteLine(" This is the old method! ");
        }
        static void New(){
          Console.WriteLine(" This is a new method! ");
        }
        public static void Main(){
          Old();
        }

3. Custom Features (Inheriting Attribute)

eg:


[AttributeUsage(AttributeUsages.Class|AttributeUsages.Method,AllowMultiple=true)]
      [AttributeUsage(AttributeUsages.Class)]  // Can only be used in a class 
      [AttributeUsage(AttributeUsages.Method)]  // Can only be used in methods 
      [AttributeUsage(AllowMultiple=true)]    // Can be found in the same 1 Multiple uses on classes 
      
      class DescriptionAttribute:Attribute{
        public string name{get;set;}
        public DescriptionAttribute(){}
        public DescriptionAttribute(string name){
          this.name = name
        }
      }

Related articles: