Home >Backend Development >PHP Tutorial >Simple knowledge points you should know about C#_PHP tutorial
1. Local variables
When you look at this title, you may be stunned, what is this? Let’s look at a small example:
static void main()
{
int a=10;
MyClass mc=new MyClass();
}
Haha, a and mc here are local variables, which, like fields, also store data. Fields usually hold data related to the state of an object, while creating local variables is often used to hold local or temporary data. Haha, simple, but what is the difference between it and instance fields:
Instance fields
Local variables
Lifetime starts from instance creation until the instance is no longer accessed
Starting from when it is declared in the block and ending when the block is executed
Implicit initialization: Initialized to the default value of the class. There is no implicit initialization. If a variable is not assigned a value before use, the compiler will report an error
Storage area Since instance fields are members of a class, all fields are stored in the heap, regardless of whether it refers to a type or a reference type. Value type: stored in the stack
Reference type: references are stored on the stack and data are stored on the heap
2.var keyword
We know that var can automatically infer the type of the variable. As shown in the above code, we can find that when the type name is provided at the beginning of the declaration, the compiler can infer its type from the right side of the initialization statement. Therefore, at the beginning of the declaration, include The type name shown is redundant.
To avoid this redundancy, you can use the new keyword var at the beginning of the declaration where the type name is displayed. The above code can be changed to:
static void main()
{
var a=10;
var mc=new MyClass();
}
3. Value parameter
Using value parameters, data is passed to the method by copying the value of the actual parameter to the formal parameter. When the method is called, the system does the following:
Allocate space on the stack for formal parameters
Copy the value of the actual parameter to the formal parameter
4. Reference parameters (ref)
No memory will be allocated on the stack for formal parameters
The actual situation is that the parameter name of the formal parameter will be used as an alias of the actual parameter variable, pointing to the same memory location
Actual parameters must be variables and must be assigned a value before being used as actual parameters. If it is a reference type variable, you can assign a reference or null
5. Output parameters (out)
Inside a method, output parameters must be assigned values before they can be used. This means that the initial value of the parameter has no effect, so there is no need to assign a value to the parameter before the method call.
Before the method returns, any path inside the method must be assigned a value for all output parameters.
The following code:
public void Add(out int outValue) { int var1=outValue+2; }If the output parameters of the above code are read before the method is assigned, an error will be reported.
6. Attributes (set, get)
The property is a function member
It does not allocate memory for data storage
It can execute code
The set accessor has a single implicit value parameter named Value, which is of the same type as the property and has a return type void
The get accessor has no parameters and has a return type that is the same as the property type.
7. Object initialization statement
We all know that creating an object consists of new followed by a class constructor and its parameter list.
The object initialization statement extends the creation syntax and places a set of member initialization statements at the end of the expression. Allows us to set the values of fields and properties when creating a new object instance.
This syntax has two forms: one form includes the parameter list of the constructor, and the other does not include it. Note that the first form can omit the parentheses.
new TypeName {FieldOrProp=InitExpr,FieldOrProp=InitExpr,…}
new TypeName {FieldOrProp=InitExpr,FieldOrProp=InitExpr,…}
Examples are as follows:
public static void main()
{
Point p1=new Point();
Point p2=new Point{X=4,Y=5,Z=6};
Point p3=new Point(9){X=7,Y=8};
}
public class Point
{
public int X=1;
public int Y=2;
public int Z=3;
public Point(int z) { Z=z; }
}
8.this keyword
The this keyword is used in the class and is a reference to the current instance. It can only be used in the code of the following class members
Instance constructor
Instance method
Instance access to properties and indexers
Since static members are not part of the instance, the this keyword cannot be used in the code of any static function member. More appropriately, this is used for the following purposes:
Used to distinguish class members and local variables or parameters
As an actual parameter of the calling method