Optional parameters, naming parameters, special types and features of. NET, optional. net

Source: Internet
Author: User

Optional parameters, naming parameters, special types and features of. NET, optional. net

1. Optional and named Parameters
1.1 optional parameters
Syntax:
[Modifier] return type method name (required parameter n, optional parameter n)
Note:
1. The required parameters can either not exist or have multiple parameters. The optional parameters can have one or more
2. Optional parameters must be placed after required parameters
3. The optional parameter must be assigned an initial value during definition.
4. Separate optional parameters.
5. When you call an optional parameter, the value cannot be assigned across parameters.
1.2 naming Parameters
Syntax:
Method Name (parameter 1: parameter 1 value ,...)
Advantages:
1. You can assign values to parameters by specifying their names.
2. You can assign values across parameters.
2. Special. NET types
2.1 implicit type
Keyword var: variables declared using var are called implicit type variables.
Note:
1. If var is used, values must be assigned when the variable is declared.
2. The implicit type cannot be assigned a value of another type after the value is declared.
2.2 anonymous type
Syntax: var stu = new {Name = "James", Age = 18, Gender = "male "};
Create an anonymous type: Create an anonymous type using the new operator and the initial object value. You do not need to define the attribute type. The attribute type is inferred from the attribute value.
Two anonymous usage methods:
1. Use var to define anonymous Variables
2. It is passed directly as the parameter value of the method. A typical use is the defalut parameter of the routes. MapRoute () method.
Use data through objects:
Var stu = new {object. Name, object. Age}; by default, the attribute Name of the used object is used as its attribute Name.
Note:
In the anonymous type, attribute values are assigned once, that is, the attributes of an anonymous object are read-only.
2.3dynamic type
Dynamic type object:
Dynamic personl = new Student {Name = "Zhang San", Age = 20 };
Personl. Introduce ();
Note:
1. When a member of the dynamic type is called, it does not perform a compile-time check, but a runtime check. Therefore, there is no quick prompt during the call.
The dynamic type is weak, and any type of value can be assigned directly.
Differences between dynamic and var keywords:
Var:
1. It can only be used for local variables and cannot be used for fields or parameters.
2. Both declarations must be initialized.
3. The variable type is determined during compilation.
Dynamic:
1. It can be used for fields of the type, method parameters, method return values, and generic type parameters.
2. values can be assigned or assigned to any type, and no forced type conversion is required.
What are the application scenarios of dynamic?
The usage of dynamic types mainly includes:
1. multi-language mixed programming, such as IronRuby and IronPython
2. Simplify reflection operations
3. Simplified COM interoperability
4. Process html dom objects
2.4 null type
Syntax:
System. Nullable <type> variable name
Type? Variable name
(Type refers to the value type)
Purpose:
1. The available type allows variables to contain undefined values, which are mostly used for interaction with databases.
2. the null type is used to solve the problem of non-null default values.
Note:
If an empty type is assigned to a non-empty type, a compilation error is thrown.
The following attributes can be used with an empty type to solve the problem:
1. DBnull. Value: indicates the null Value of the database.
Case: int? Age user. Age = reader ["Age"]! = DBnull. Value? (Int ?) Reader ["Age"]: null;
2. HasValue: belongs to the bool type. When a variable contains non-null values, it is set to true.
3. Value: the type is the same as its corresponding Value type. If HasValue is true, the Value contains a meaningful Value. If HasValue is false, access Value will cause InvalidOperaionException
Use "?? "Default operator allocation value:
Int? Num = null;
Int num2 = num ?? 0;
"?? "The operator first checks whether the left operand is null. If it is null, return "?? "Default value given on the right, otherwise the left operand is returned.
3. Features
Syntax: [feature name] or [feature name (parameter value...)]
Main features in C:
1. Add additional information to the target element (which can be an assembly, class, attribute, method, etc.), similar to the Annotation
2. A feature is essentially a class that inherits directly or indirectly from the Attribute class.
Note:
1. feature names all end with Attribute, which can be omitted during use.. NET will automatically find the corresponding feature class
2. Multiple objects can be used for the same target element.
3. The parameter names must be placed between [] and () are determined by the constructor of the feature class.
1. features used in methods
ObsoleteAttribute: used to mark a program element that is no longer in use.
[Obsolete ("do not use the old method, please use the new method", false)]
The 1st parameters are string-type text descriptions and explanations.
The 2nd parameter indicates whether the compiler regards using this method as an error. If the value is true, an error is prompted during compilation. If the value is false, the program will not report an error, however, the compiler generates a warning.
The Obsolete feature is the ObsoleteAttribute class inherited from the Attribute class. It contains constructors and Attribute members. According to the definition of the Obsolete feature, there can be multiple syntax forms when using it:
[Obsolete] and [Obsolete ("do not use the old method, please use the new method", false)]
[Obsolete] and [Obsolete ()] are equivalent.
2. The features are applied to the class:
[Serializable]: Support for serialization
Serialization mainly refers to converting the object state information into a form that can be stored or transmitted
3. The feature is applied to the Assembly.
All. NET projects contain the features applied to the Assembly, which appear in the "Properties \ AssemblyInfo. cs" folder of all projects.
AssemblyTitle: indicates the Assembly title.
AssemblyDescription: used to describe assembly information
AssemblyCompany: used to define a company
AssemblyProduct: used to define a product
AssemblyCopyright: Used to declare the version
GuidL: used to define the unique number of an assembly.
AssemblyVersion: defines the assembly version.
Is there only one feature for a target element (class or attribute?
Not necessarily, the target element in C # can have multiple features.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.