- The #region and #endregion keywords allow you to comment on how the code is broken into several fragments, and you can expand and collapse the section of the code area.
- Aliases and range of values for basic types:
The above number is preceded by a letter, such as the float value must be followed by F, otherwise it will be treated as a double by the compiler.
Unicode escape sequences can be used to specify Unicode characters, which include the standard \ character followed by a U and a 4-bit hexadecimal value (for example, Unicode is 0x0027 for single quotes). The following string is equivalent:
"Karli\ ' s string."
"Karli\u0027s string."
string preceded by an @ character, that is, all characters between two double quotation marks are included in the string, including the end of line character and the character to be escaped. The only exception is the escape of the double-quote character, which must be specified to avoid ending the string.
The using statement can also provide an alias for the namespace.
1 namespace Levelone 2 3 using LT = Leveltwo; 4 Name "Namethree" defined 5 namespace Leveltwo 6 { 7 // name "Namethree" defined 8 Span style= "color: #008080;" >9 }
The code in the Levelone namespace can refer to Levelone.namethree as Namethree and LevelOne.LevelTwo.NameThree to Lt.namethree.
In C + +, you can run another case statement after you run out of one case statement. C # No, the code in each branch must end with a break, unless multiple case statements are put together:
Switch (<testVar>) { case <comparisonVal1>: Case < Comparisonval2>: <code to execute> break ; ...}
- Checked and unchecked, called an overflow check context for an expression.
- Convert string to enumeration: (Enumerationtype) Enum.parse (typeof (Enumerationtype), enumerationvaluestring);
string " North " = (Orientation) enum.pase (typeof(Orientation), myString);
- If you use a variable to define an array size, the variable must be a constant.
- The Foreach Loop has read-only access to the array contents, so you cannot change the value of any element. For example, you cannot write the following code
foreach (string in friendnames) { "Rupertthe Bear";}
- Multidimensional arrays: such as two-dimensional, <basetype>[,] <name>; Visit hillheight[2,1]
Array of arrays (jagged arrays or indefinite arrays):
int [] Jaggedintarray;
Initialization
intnewint[2][];jaggedintarray[0newint [3];jaggedintarray[1newint[4];
Or:
New int [3] []{newint[]{1,2,3}, newint[]{ 1 }, newint[]{1,2}};
Or:
int [] Jaggedintarray = {newint[]{1,2,3}, new int []{1}, newint[]{1,2}};
An array of arrays (jagged arrays or indefinite arrays) can use a Foreach loop, but an error is used as follows (normal two-dimensional arrays can be): There is an array divisors1to10 is an indeterminate array:
foreach (int in divisors1to10) { Console.WriteLine (divisor);}
Because the array divisors1to10 contains the int[] element, not the int element. Each subarray and the array itself must be looped:
foreach (int in divisors1to10) { foreach(int in Divisorofint) { Console.WriteLine (divisor); }}
- C # allows you to specify one (only one) specific parameter for a function, which must be the last parameter in the function definition, called a parameter array. parameter arrays can call functions with variable numbers of parameters, which can be defined using the params keyword. Such as:
Function references: REF, which is defined and used with ref:
static void showdouble (ref int val);
Showdouble (ref MyNumber);
Output parameters: Out, used in the same way as the REF keyword (modifiers used as parameters in function definitions and function calls). But note:
1. It is illegal to use an unassigned variable as a ref parameter, but an unassigned variable can be used as an out parameter.
2. In addition, when the function uses an out parameter, the out parameter must be considered as not yet assigned.
That is, the calling code can use an assigned variable as an out parameter, and the value stored in the variable is lost when the function executes.
- Delegate: A function pointer in C with delegate in front of the function prototype when defining the type
Output debugging Information: Writes the text to the Output window during run time.
1.debug.writeline (), run only in debug mode, the command disappears in the release version
2.trace.writeline (), which can be used to publish programs
There are also similar:
Debug.Write ()
Trace.Write ()
Debug.WriteLineIf ()
Trace.WriteLineIf ()
Debug.WriteIf ()
Trace.writeif ()
The decision statement (assertion) is interrupted:
Debug.Assert ()
Trace.Assert ()
- Try ... finally: only a try block and a finally block, no catch block, or a try block and several catch blocks. If there is one or more catch blocks, the finally block is optional, otherwise it is required.
Static constructors: A class can have only one static constructor, which cannot have an access modifier or take any parameters. Static constructors cannot be called directly and can only be performed in the following cases:
? When you create a class instance that contains a static constructor
? When accessing a static member of a class that contains a static constructor
No matter how many instances of a class are created, their static constructors are called only once.
- Static classes: Classes contain only static members and cannot be used to instantiate objects such as the console.
An interface is the combination of public instance (non-static) methods and properties to encapsulate a collection of specific functionality. Interfaces cannot exist alone. You cannot instantiate an interface as you instantiate a class. In addition, an interface cannot contain any code that implements its members, but can only define the member itself. The implementation process must be done in the class that implements the interface. The name of the interface usually begins with the capital letter I (e.g. Ihotdrink)
Removable object: An object that supports the IDisposable interface must implement its Dispose () method. C # allows the use of a structure that optimizes the use of this method. The Using keyword can initialize an object in a code block that uses an important resource, and automatically calls the Dispose () method at the end of this block of code, using the following:
Alternatively, initialize as <VariableName> as part of the using statement:
In both cases, the variable <variablename> can be used in the using code block, and automatically deleted at the end of the code block (call Dispose () after the code block has finished executing).
Modifiers that can be added when defining a class:
Internal: The class is declared as internal, that is, only the code in the current project can access it. The default is the value if not added.
Public: Classes are common and should be accessible by code in other projects.
It cannot be protect or private, and there are two optional mutex modifiers:
Abstract or sealed: abstracted (cannot be instantiated, only inherited, can have abstract members) or sealed (sealed, cannot inherit).
The following table is a combination of the access modifiers that can be used in a class definition:
- If a base class is specified, it must be immediately after the colon, followed by the specified interface. If you do not specify a base class, the interface is followed by a colon. You must use commas to separate the base class name (if there is a base class) and the interface name.
The interface definition is basically the same as the class, except that you cannot use the keyword abstract and sealed in the interface
The combination uses GetType () and typeof (which is a C # operator that converts the class name to a System.Type object) and can be compared as follows:
- No matter what constructors are used on a derived class (the default constructor or a non-default constructor), the default constructor for the base class is used unless explicitly specified. You can specify the base class constructor that is used in the constructor definition of a derived class, which specifies that the. NET instantiation process uses a constructor with the specified parameters in the base class. The following example invokes a constructor with a parameter in the base class: (If the constructor initializer is not specified for the constructor, the compiler automatically adds base () to call the default constructor)
In addition to the base keyword, this can also be used as the constructor initializer for the other keyword. This keyword specifies that the. NET instantiation process uses a non-default constructor for the current class before invoking the specified constructor. For example: (You typically use a constructor initializer to specify only one constructor.) )
This code executes the following sequence:
Executes the System.Object.Object constructor.
Executes the mybaseclass.mybaseclass (int i) constructor.
Executes the myderivedclass.myderivedclass (int i, Int j) constructor.
Executes the Myderivedclass.myderivedclass () constructor.
A struct is a value type, and a class is a reference type, while passing a class variable actually passes a pointer, and the operation of one variable also alters the value of another variable.
ReadOnly, which indicates that this field can be assigned only during the execution of a constructor, or by an initialization assignment statement. For example:
Accessors, defined by the Get and set keywords, can be used to control the level of access to a property. You can omit one of the blocks to create a read-only or write-only property (omit the Get block to create a write-only property, ignoring the set block to create a read-only property). Define a property as follows:
- Simple properties are generally associated with private fields to control access to this field, using the keyword value to represent the user-provided property value:
overridden or hidden base class method: If the inherited member is virtual, you can rewrite the implementation code with the override keyword. Regardless of whether the inherited member is virtual, you can hide these implementation codes with the New keyword (not mandatory, without warning).
Where the overriding method replaces the implementation code in the base class, even if this is done through a base class type, the new version will be used:
For the virtual and non-virtual methods of the base class, although the implementation code of the base class is hidden, it can still be accessed through the base class: Change only one row of the example above, and the result becomes base imp
The definition of an interface member is similar to the definition of a class member, but there are several important differences:
? Access modifiers (public, private, protected, or internal) are not allowed, and all interface members are common.
? An interface member cannot contain a code body.
? An interface cannot define a field member.
? Interface members cannot be defined with the keyword static, virtual, abstract, or sealed.
? The type definition member is forbidden.
But to hide the members that inherit the base interface, you can define them with the keyword new, for example:
- The
- class that implements the interface must contain implementation code for all members of the interface, and must match the specified signature (including matching the specified get and set blocks) and must be public, using the keyword virtual or abstract to implement the interface member, but not using the static or const. You can also implement interface members on a base class, for example:
Inherits a base class that implements a given interface, which means that the derived class implicitly supports this interface. If the implementation code is defined as virtual in the base class, the derived class can replace the implementation code instead of hiding it. If you want to use the New keyword to hide a base class member instead of overriding it, the method imyinterface.dosomething () always references the base class version, even if the derived class is accessed through this interface.
The above example is an implicit implementation of an interface member, which can be visited by classes and interfaces:
or:
can also explicitly implement interface members by a class. If you do this, the member can only be accessed through the interface, not through the class:
Where dosomething () is explicitly implemented, and Dosomethingelse () is implicitly implemented. Only the latter can be accessed directly from an object instance of MyClass. The
said earlier that if you implement an interface with attributes, you must implement a matching get/set accessor. This is not absolutely true-if you include only set blocks in the interface that defines the properties, you can add a get block to the properties in the class and vice versa. However, this can only be done if the accessible modifier of the accessor that you have added is more restrictive than the accessibility modifier of the accessor that is defined in the transmitting connector. Because by definition, the accessors of an interface definition are public, that is, only non-public accessors can be added. For example:
Partial class Definitions: When you create a class that contains many members of a type or other type, it is easy to confuse, and the code file is longer when you can use a partial class definition to place the definition of a class in multiple files. For example, you can put fields, genera
And the constructor is placed in one file, and the method is placed in another file. To do this, you only need to use the partial keyword for the class in each file that contains a partial class definition, as follows:
If you use a partial class definition, the partial keyword must appear in the same location as each file that contains the definition section. An interface applied to a partial class will also be applied to the entire class, i.e. the following two definitions are equivalent:
A partial class definition can contain a base class in a partial class definition file or in several partial class definition files. However, if the base class is specified in more than one definition file, it must be the same base class, because in C #, a class can inherit only one base class.
Part of the method definition, some methods are defined in a partial class, but there is no method body, and the implementation code is included in the other part of the class. Some methods can also be static, but they are always private and cannot have a return value. Any parameters they use cannot be out parameters, but can be ref parameters. Some methods also cannot use virtual, abstract, override, new, sealed, and extern modifiers. With these limitations, it is not easy to see how some of these methods work. In fact, some methods are very important when compiling code, and their usage is not important. Consider the following code:
When calling DoSomething in a console application, the output is as follows:
If you delete the second part of the class definition, or delete all of the execution code of the partial method (commented out code), the output is as follows
As shown:
When compiling the code, if the code contains a partial method that does not implement the code, the compiler completely deletes the method and removes all calls to the method. When the code is executed, the implementation code is not checked because the call to the method is not checked. This will slightly improve performance. As with some classes, some of the methods are useful when customizing code that is automatically generated or created by the designer. The designer declares some methods, and the user chooses whether or not to implement it, depending on the situation. If you do not implement it, performance is not affected because the method does not exist in the compiled code. Therefore, some methods cannot have return types.
A boxing is the conversion of a value type to a System.Object type, or to an interface type implemented by a value type.
Unpacking (unboxing) is the opposite of the conversion process.
You can also marshal value types into an interface type, as long as they implement this interface:
The IS operator is not a way of stating that an object is a type, but rather to check whether the object is of a given type, or whether it can be converted to a given type, and if so, this operator returns True:<operand> is <type>
If <type > is a class type, and <operand> is the type, or it inherits the type, or it can be marshaled to the type, the result is true.
If <type > is an interface type, and <operand> is the type, or if it is the type that implements the interface, the result is true.
If <type > is a value type, and <operand> is the type, or it can be disassembled into that type, the result is true.
The AS operator uses the following syntax to convert a type to the specified reference type: <operand> as <type>
This applies only to the following situations:
? The type of <operand> is <type > type
? <operand> can be implicitly converted to <type > type
? <operand> can be sealed to <type > type
If you cannot convert from <operand> to <type, the result of an expression is null
Define nullable types: Generics use the system.nullable<t> type to provide a way to make the value type NULL. For example:
System.nullable<int> Nullableint;
Int? is an abbreviation for system.nullable<int>, but it is easier to read:
Int? Nullableint;
If OP1 is null, an exception of type System.InvalidOperationException is generated.
?? Operator: null join operator (null coalescing operator), is a two-tuple operator that allows for possible equal to NULL
Provides another value for the expression. If the first operand is not NULL, the operator is equal to the first operand, otherwise the operator is equal to the second operand. The following two expressions have the same effect:
Object initializer, which is how you can instantiate and initialize an object without adding additional code to the class, such as the constructor described here:
Such as:
- You can implement an iterator that controls how the loop code is fetching values during its loop. To iterate a class, you need to implement the GetEnumerator () method whose return type is IEnumerator. To iterate over a member of a class, such as a method, you can use the IEnumerable return type. In the code block of the iterator, use the yield keyword to return the value. Such as: