Optional parameters, named parameters,. NET special types, features

Source: Internet
Author: User

1. Optional parameters and named parameters
1.1 Optional parameters
Grammar:
[Modifier] Return type method name (required parameter n, optional parameter n)
Attention:
1. The required parameters may not exist, or can have multiple, optional parameters can have 1 or more
2. Optional parameters must be placed after required parameters
3. Optional parameters need to be assigned an initial value when defined
4. Optional parameters also need to be used, separated
5. When calling optional parameters, you cannot assign values across parameters
1.2 Naming parameters
Grammar:
Method Name (Parameter 1: Parameter 1 value,...)
Advantage:
1. Parameter assignment can be given by specifying the name of the parameter
2. You can assign values across parameters
2,. The special types of net
2.1 Implicitly-typed
Keyword var: variable declared with VAR is called an implicit type variable
Attention:
1. Using Var, you must also assign a value when declaring a variable
2. An implicit type cannot give a value to another type after it has been declared assigned
2.2 Anonymous Types
Syntax: var stu=new {name= "Zhang San", age=18,gender= "male"};
Creation of anonymous types: Create anonymous types with the new operator and object initializer, no need to define the type of the property, attribute type inferred from the property value
Two ways to use anonymous types:
1. Using var to define anonymous type variables
2. Pass directly as the parameter value of the method, the typical use is routes. The Defalut parameter of the MapRoute () method
Working with data by object:
var stu=new {object. Name,object.    AGE}; By default, the property name of the object being used as its property name
Attention:
The assignment of an attribute in an anonymous type is one-time, that is, the property of the anonymous object is read-only
2.3dynamic type
Dynamic type Object:
Dynamic Personl=new student{name= "Zhang San", age=20};
Personl. Introduce ();
Attention:
1. When invoking a member of a dynamic type, it does not do compile-time checking, but is checked at run-time, so there is no prompt when calling
The dynamic type is a weak type and can be directly assigned to any type of value
The difference between the dynamic and the VAR keyword:
Var:
1. Can only be used for local variables, not for fields, parameters
2. The declaration must be initialized at the same time
3. The type of the variable is determined at compile time.
Dynamic
1. Fields, method parameters, method return values that can be used for a type, such as generic type parameters
2. Can be assigned or assigned to any type and does not require coercion of type conversions
What are the application scenarios for dynamic?
The use of dynamic types is mainly:
1. Multi-language mixed programming, such as IronRuby and IronPython
2. Simplifying the reflection operation
3. Simplifying COM Interop
4. Working with HTML DOM objects
2.4 Nullable types
Grammar:
system.nullable< type > variable name
Type? Variable name
(type refers to a value type)
Role:
1. Available types allow variables to contain undefined values and are used in interactions with the database
2. Nullable type is to solve the problem of non-null type default value
Attention:
A compilation error is thrown when a nullable type is assigned to a non-null type
The following properties work with nullable types to resolve the problem:
1.dbnull.value: Represents a null value for the database
Case: int? Age user. age=reader["Age"]!=dbnull.value? (int?) reader["Age"]:null;
2.HasValue: is of type bool and is set to True when the variable contains a non-null value
3.Value: The type is the same as its corresponding value type, and how HasValue is True indicates that value contains a meaningful value. If HasValue is false, accessing value throws a Invalidoperaionexception exception
Use the "??" operator to assign a default value:
Int? Num=null;
int num2=num?? 0;
"??" The operator first determines whether the left operand is null, or returns "??" if null. The default value given on the right, otherwise returns the left operand
3. Characteristics
Syntax: [attribute name] or [special name (parameter value ... )]
Features in C # are the main features:
1. Add additional information to the target element (which can be an assembly, class, property, method, and so on), similar to a comment
2. Nature is also a class, directly or indirectly inherited from the attribute class
Attention:
1. Attribute naming all end with attribute, can be omitted when used. NET will automatically find the corresponding attribute class
2. The same target element can use multiple
3. The attribute name must be placed between [], the parameter between () is determined by the constructor function of the attribute class
1. Features for methods
ObsoleteAttribute: It is 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 1th parameter is a literal description of the string type, interpreted.
The 2nd parameter is to tell the compiler whether to use this method as an error, and if the value is true, the error will be prompted at compile time, and if False, the program will not error, but the compiler will produce a warning
The obsolete attribute is the ObsoleteAttribute class, inherited from the attribute class, which contains constructors and property members, which can be used in a variety of grammatical forms, depending on the definition of the obsolete attribute:
[Obsolete] and [Obsolete ("Do not use the old method, please use the new method", false)]
[Obsolete] and [Obsolete ()] are equivalent
2. Attributes are applied to the class:
[Serializable]: indicates support for serialization
serialization, which converts the state information of an object into a form that can be stored or transmitted.
3. Attributes are applied to the assembly
At all. NET project contains the attributes that are applied to the assembly, which appear in the "Properties\assemblyinfo.cs" folder in all projects
Assemblytitle: Represents the title of an assembly
Assemblydescription: Used to describe Assembly information
Assemblycompany: Used to define a company
Assemblyproduct: Used to define the product
Assemblycopyright: Used to declare a version
Guidl: The number used to define an assembly unique
AssemblyVersion: Defining the version of an assembly
is a target element (class, attribute) can have only one attribute?
Not necessarily, the target element in C # can have multiple attributes

Optional parameters, named parameters,. NET special types, features

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.