Home > Backend Development > C#.Net Tutorial > Detailed explanation of parameters and special types of instances in ASP.NET

Detailed explanation of parameters and special types of instances in ASP.NET

零下一度
Release: 2017-07-02 11:58:36
Original
1663 people have browsed it

This article mainly introduces the parameters and special types and characteristics in ASP.NET. It is very good and has reference value. Friends in need can refer to it

1. Optional parameters and named parameters

## 1. Optional parameters

Syntax:


[Decision] Return the type method name (must be selected parameter 1 ... must choose parameter n, optional parameter 1 ... available parameters n)

## eg:

static void BookList(string category,int pageIndex = 1)
        {
          //操作
        }
        //调用
        static void Main(string[] args)
        {
          BookList("C#"); //使用全部默认参数
          BookList("C#",2) //不使用默认参数
        }
Copy after login

2. Named parameters

Syntax:


Method name (parameter 1 name: parameter 1 Value...Parameter n name: Parameter n value)                                                                                                                                 Special types


                                                                                                                                                      to , foreach initialization statement, using statement                                                                                                                                                                                             

                                                                                                                                           

1 name: Attribute 1 value,...Attribute n name: Attribute n value}                                                                                                            The assignment of attributes in anonymous types is one-time, that is, the attributes of anonymous pairs are read-only.


# will not report an error when compiling, and an error will be reported at runtime, because there is no INTRODUCE method

#4, Dynamic and VAR keywords. ?

          var can only be used for local variables, not fields and parameters; it must be initialized at the time of declaration; the type of the variable is determined at compile time Fields, method parameters, method return values, can be used for generic type parameters, etc.; can be assigned or assigned to any type                                                                                                                                                                                                Can be assigned or assigned to any type

5. Nullable type

                              1. Syntax:                                                                                                                                                                                                                                                

# or

类型?变量名

eg:


 System.Nullable<int> num = null;
             System.Nullable<DateTime> birthday = null;
            //或
             int? num = null;
            DateTime? birthday = null;
Copy after login

注:当把一个可空类型赋值给一个非空类型时,将引发编译错误


   eg:int? num = null; int num2 = num;
Copy after login

2.可以使用可空类型的属性,解决不能把一个可空类型赋值给一个非空类型

(1) HasValue:属于bool类型,当变量包含非空值时,它被设置为true

(2) Value:如果HasValue为true 则说明Value包含有意义的值,否则将引发InvalidOperaionException


   int? num1 = 5
        int num2 = num1??0;
Copy after login

三、特性

1.C#的特性主要有以下特点

为目标元素(可以是程序集、类、属性、方法)加入附加信息,类似于注释

特性本质上也是一个雷,直接或间接的继承自Acttribute类

特性命名都以Attribute结尾,但是在使用它时可以省略,.NET会自动找到对应得特性类

2.语法

[特性名] 或[特性名(参数值...)]

eg:


[Obsolete]  //这个方法可用使用 但是编译时会警告
        [Obsolete("不要使用旧的方法,使用新的方法",false)]  //这个方法可用使用 但是编译时会警告
        [Obsolete("不要使用旧的方法,使用新的方法",true)]   //这个方法不可以使用,编译时会报错
        static void Old(){
          Console.WriteLine("这是旧方法!");
        }
        static void New(){
          Console.WriteLine("这是新方法!");
        }
        public static void Main(){
          Old();
        }
Copy after login

3.自定义特性(继承Attribute)

eg:


[AttributeUsage(AttributeUsages.Class|AttributeUsages.Method,AllowMultiple=true)]
      [AttributeUsage(AttributeUsages.Class)]  //只能在类中进行使用
      [AttributeUsage(AttributeUsages.Method)]  //只能在方法中进行使用
      [AttributeUsage(AllowMultiple=true)]    //可以在同一个类上进行多次使用
      
      class DescriptionAttribute:Attribute{
        public string name{get;set;}
        public DescriptionAttribute(){}
        public DescriptionAttribute(string name){
          this.name = name
        }
      }
Copy after login

The above is the detailed content of Detailed explanation of parameters and special types of instances in ASP.NET. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template