1, structural function characteristics? Implementation code?
Structs are defined with struct keywords, similar to classes but intrinsically different. The structure essence is a value type and it does not need to be assigned.
Characteristics of the structure:
(1), when the structure is passed as a parameter, the value is passed.
(2), the structure of the constructor must have parameters.
(3), the structure instantiation can not be new.
(4), structure cannot inherit, but can implement interface.
(5), the instance field cannot be initialized in the struct.
Cases:
usingSystem;usingSystem.Collections.Generic;usingSystem.Text; namespaceteststruct {classProgram { Public structCircle//Define a circle { Private Const DoublePI =3.1415926; Public DoubleRadius//radius /// <summary> ///constructor Function/// </summary> PublicCircle (Doubler) {radius=R; } /// <summary> ///Area/// </summary> Public DoubleCarea () {return 3.14* RADIUS *radius; } } Static voidMain (string[] args) {Circle circle1; //Do not instantiate with newCircle1.radius=5; Console.WriteLine ("The Circle area is:"+Circle1. Carea ()); Circle Circle2=NewCircle (1);//Instantiate with newConsole.WriteLine ("The Circle area is:"+Circle2. Carea ()); Console.ReadLine (); } } } usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceteststruct{classProgram { Public structCircle//Define a circle { Private Const DoublePI =3.1415926; Public DoubleRadius//radius /// <summary> ///constructor Function/// </summary> PublicCircle (Doubler) {radius=R; } /// <summary> ///Area/// </summary> Public DoubleCarea () {return 3.14* RADIUS *radius; } } Static voidMain (string[] args) {Circle circle1; //Do not instantiate with newCircle1.radius=5; Console.WriteLine ("The Circle area is:"+Circle1. Carea ()); Circle Circle2=NewCircle (1);//Instantiate with newConsole.WriteLine ("The Circle area is:"+Circle2. Carea ()); Console.ReadLine (); } }}View Code
2. What is a delegate? Characteristics? When do I use delegates instead of interfaces? How do I declare, instantiate, and use a delegate?
(1), a delegate is a class that defines the type of the method, which can be used as a parameter to another method. Avoid using branches in your programs,
Program extensibility is better.
Example:
classProgram { Public Delegate voidPrintedelegate (stringname); Private Static voidPrinteenglish (stringname) {Console.WriteLine ("Your Name:"+name); } Private Static voidPrintechinese (stringname) {Console.WriteLine ("Your name:"+name); } Private Static voidPrinte (stringname, Printedelegate makegreeting) {makegreeting (name); } Static voidMain (string[] args) {Printe ("Sam Young", Printeenglish); Printe ("Poplar", Printechinese); Console.ReadLine (); } } classProgram { Public Delegate voidPrintedelegate (stringname); Private Static voidPrinteenglish (stringname) {Console.WriteLine ("Your Name:"+name); } Private Static voidPrintechinese (stringname) {Console.WriteLine ("Your name:"+name); } Private Static voidPrinte (stringname, Printedelegate makegreeting) {makegreeting (name); } Static voidMain (string[] args) {Printe ("Sam Young", Printeenglish); Printe ("Poplar", Printechinese); Console.ReadLine (); } }View Code
(2), the delegate is similar to a C + + function pointer, but it is type-safe. The
Delegate allows the method to be passed as a parameter. The
delegate can be used to define a callback method.
delegates can be chained together; For example, multiple methods can be called on an event. The
method does not need to match the delegate signature exactly. For more information, see Covariance and contravariance.
C # version 2.0 introduces the concept of anonymous methods, which allow code blocks to be passed as parameters in place of a separately defined method.
using delegates, I think it should be used when different methods need to be called by the branch. However, for example, in Factory mode, different classes
are instantiated according to the branch, and then the interface is used.
The
delegate is a class that defines the type of the method so that the method can be passed as a parameter of another method, which moves the method
, and the
State assigns to the parameter You can avoid a large number of if-else (Switch) statements in your program, while making your program more extensible
Malleable.
3. What is a partial class/partial class? What are the features? Implementation code? Where is the application? How many rules do you need to follow? The
partial class divides a class into several separate files with the partial keyword, but in essence it is a class. Generally, the form Form.cs and Form.designer.cs are the most common when there are too many rows in a class or some functions can be separated by partial classes. The
generally follows the following rules:
(1), you must use the partial keyword
(2), although there are different parts, each part must have the same accessibility, such as public, private, and so on
(3), if any part is declared abstract, sealed, then the entire type is considered abstract, sealed
,
(4), if any part of the declaration inherits the base class, the entire type will inherit the class
(5), Each section can specify a different base interface, and the final type will implement all the interfaces listed by all the partial declarations
(6), any class, struct, or interface member declared in a partial definition is available to all other parts using
(7), Nested types can be partial, even if the type in which they are nested is not part of the division itself.
C # classes and structs (1)