Class and Structure
Class is the reference type stored on heap, and the structure is the value type stored on stack.
Structure does not support inheritance
Class
Data members are members of fields, constants, and events that contain Class data.
Function members provide certain functions for operating data, including methods, attributes, constructors, finalizer, operators, and indexers.
The ref parameter forces the value parameter to pass to the method through reference.
When the out prefix is added before the input parameter of the method, the variables passed to the method can be not initialized.
Naming parameters can be passed in any order
Parameters can also be optional. The default value must be provided for optional parameters.
Method overload:
The two methods cannot be different only in the return type.
The two methods cannot be distinguished only based on whether the parameter is declared as ref or out.
Attribute
If the set accessor is omitted in the attribute definition, you can create a read-only attribute.
If the get accesser is omitted in the attribute definition, you can create a write-only attribute.
C # allow different access modifiers for the set and get accessors of the attribute. The attribute can have public get accessors and private or protected set accessors.
If the set and get accessors of the attribute do not have any logic, you can use the automatically implemented attribute.
Constructor
Note: If a constructor with parameters is provided, the compiler will not automatically provide the default constructor.
You can define the constructor as private or protected, so that irrelevant classes cannot be accessed,
This is useful in the following two cases:
The. Class is only used as a container for some static members or attributes, so it will never be instantiated.
. The class is expected to be instantiated only by calling a static member function (this is the so-called class factory method for Object Instantiation)
Static constructor. The class has some static fields or attributes. You need to initialize these static fields and attributes from the external source before the first use.
C # The constructor can call another constructor of the same class or call a constructor of a direct base class (using the same syntax, use the base keyword instead of this)
Read-Only Field
Readonly
You can assign values to read-only fields in the constructor, but not elsewhere.
public class Document{ public readonly DateTime CreationData public Document() { // Read in creation date from file.Assume result is 1 Jan 2002 // but in general this can be different for different instances // of the class CreationDate = new DateTime(2002,1,1); }}
Anonymous Functions
VaR can be used with the new keyword to create an anonymous type.
As follows:
var caption = new {FirstName = "James", MiddleName = "T",LastName = "Kirk"};
Structure
Positive impact: It is very fast to allocate memory to the structure because they are inline or saved in the stack. When the structure is deleted beyond the scope, the speed is also very fast.
Negative Impact: as long as the structure is passed as a parameter or a structure is assigned to another structure, all contents of the structure are copied, and only references are copied for classes.
Structure inheritance chain: each structure is derived from the system. valuetype class, and the system. valuetype class is also derived from system. object.
The default constructor initializes the value fields to 0, initializes the reference type fields to null, and always gives them implicitly.
Classification
The partial keyword allows the class, structure, or interface to be placed in multiple files.
The keyword partial is used to place partial before the class, struct, or interface keyword.
Extension MethodIt allows changing a class, but does not need the source code of the class.
C # Study Notes ---- objects and types