This article mainly introduces the parameters and special types and characteristics of ASP, very good, with reference value, the need for friends can refer to the following
One, optional parameters and named parameters
1. Optional parameters
Grammar:
[modifier] Returns the type method name (required parameter 1 ...). Required parameter n, optional parameter 1 ... Optional parameter N)
eg
static void Booklist (string category,int pageIndex = 1) { //action } //call to static void Main (string[] args) { Booklist ("C #");//use all default parameters Booklist ("C #", 2)//Do not use default parameters }
2. Named parameters
Grammar:
Method Name (Parameter 1: Parameter 1 value ...) Parameter n Name: parameter n value)
eg
static void Booklist (string category,int pageIndex = 1) { //action } //call to static void Main (string[] args) { Booklist (category: "C #");//use all default parameters Booklist (Category: "C #", pageindex:2)//Do not use default parameters }
Two. The special types of net
1. Implicit type
Implicit types are mainly used in the following applications: Declaring a local type variable, a for initialization statement, a foreach initialization statement, a using statement
eg
var list = new Student (); Student is a custom type
Note: With Var, you must assign a value while declaring a variable
2. Anonymous type (anonymous type can be created by the new operator and the initial value)
new{Property 1 Name: Property 1 Value,... Property n Name: attribute N Value}
eg
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 property of the anonymous pair is read-only
3. Type of dynamic (defining the dynamics type)
Create 1 dynamically typed objects dynamic Person1 = new Student{name= "Bing", age=20}; Person1. Introduce ();
No error at compile time, error when running, 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, and must be initialized at the same time, and the type of the variable is determined at compile time.
Dynmic is used for types of fields, method parameters, method return values, can be used for generic type parameters, etc., can be assigned or assigned to any type
You do not need to force type conversions
5. Nullable type
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: A compilation error is thrown when a nullable type is assigned to a non-null type
Eg:int? num = null; int num2 = num;
2. You can use a nullable type property to resolve that a nullable type cannot be assigned to a non-null type
(1) HasValue: type bool, when the variable contains a non-null value, it is set to True
(2) Value: Indicates that value contains a meaningful value if HasValue is true, otherwise the invalidoperaionexception will be thrown
Int? NUM1 = 5 int num2 = NUM1?? 0;
Third, characteristics
the characteristics of 1.c# mainly include the following characteristics
Add additional information to the target element (which can be an assembly, class, property, method), similar to a comment
Nature is also a ray, directly or indirectly inherited from the Acttribute class
Attribute naming ends with attribute, but can be omitted when using it. NET will automatically find the corresponding attribute class
2. Syntax
[attribute name] or [attribute name (parameter value ...)]
eg
[Obsolete] This method can be used but will be compiled with warnings [Obsolete ("Do not use the old method, use the new method", false)] //This method can be used but compile warns [Obsolete ("Do not use the old method, Use the new method ", true)] //This method can not be used, the compilation will be error static void old () { Console.WriteLine (" This is the older Method! ") "); } static void New () { Console.WriteLine ("This is the new method! "); } public static void Main () {old (); }
3. Custom attributes (Inherit attribute)
eg
[attributeusage (attributeusages.class| Attributeusages.method,allowmultiple=true)] [AttributeUsage (Attributeusages.class)]//can only be used in the class [AttributeUsage (Attributeusages.method)]//can only be used in methods [AttributeUsage (Allowmultiple=true)]//may be used on the same class multiple times with class Des criptionattribute:attribute{public string Name{get;set; Public DescriptionAttribute () {} public DescriptionAttribute (string name) {this.name = name}}