C # should know the simple knowledge point _php tutorial

Source: Internet
Author: User

A simple knowledge point that C # should know


1. Local Variables

A look at the title you may be a leng, this is a what stuff. Look at a small example:

static void Main ()

{

int a=10;

MyClass mc=new MyClass ();

}

Hehe, the A and MC here are local variables, which, like fields, also hold data. Fields typically hold data about the state of the object, while creating local variables is often used to hold local or temporary data. Oh, simple, but it is different from the instance field:

Instance fields

Local variables

Lifetime starts from instance creation until the instance is no longer accessed

Starting with it being declared in the block, to the end of the block execution

The implicit initialization is initialized to the default value of the class without an implicit initialization. If the variable is not assigned before it is used, the compiler will error

Storage area because the instance field is a member of a class, all fields are stored in the heap, whether it refers to a type or a reference type value type: stored in the stack

Reference type: Reference stored in stack, data stored in heap

2.var keywords

We know that Var can automatically infer the type of the variable, such as the code above, and we can see that the compiler can infer its type from the right side of the initialization statement when it provides the type name at the beginning of the declaration, so the first part of the declaration includes the display of the type name superfluous.

To avoid this redundancy, you can use the New keyword var in the location of the display type name at the beginning of the declaration. The above code can be changed to:

static void Main ()

{

var a=10;

var mc=new MyClass ();

}

3. Value parameters

The value parameter is used to pass the data to the method by copying the value of the argument to the formal parameter. When the method is called, the system does the following:

Allocating space for a parameter in the stack

To copy the value of an argument to a parameter

4. Reference parameters (ref)

No memory is allocated on the stack for the parameter

The fact is that the parameter name of the formal parameter is the alias of the argument variable, pointing to the same memory location

The argument must be a variable and must be assigned before it is used as an argument. If it is a reference type variable, you can assign a reference or null

5. Output parameters (out)

Inside the method, the output parameter must be assigned before it can be used. This means that the initial value of the parameter is not functional, so it is not necessary to assign a value to the parameter before the method call.

Any path inside the method must be assigned one time for all output parameters before the method returns.

The following code:

public void Add (out int outvalue) {int var1=outvalue+2;} The above code output parameter is read before the method assignment will give an error.

6. Properties (Set,get)

property is a function member

It does not allocate memory for storage of data

It can execute code

The set accessor has a separate implicit value parameter named value, which is the same as the type of 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 Statements

We all know that creating an object consists of a class constructor and a list of its arguments followed by new.

Object initialization statements extend the creation syntax and place a set of member-initialized statements at the end of an expression. Allows us to set values for fields and properties when we create new object instances.

The syntax has two forms: one is a list of arguments that include constructors, and the other is not. Note the first form can omit 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 keywords

The This keyword is used in a class and is a reference to the current instance. It can only be used in code of the following class members

Instance constructors

Instance method

Instance access rights for properties and indexers

Because static members are not part of an instance, you cannot use the This keyword in code for any static function member. More appropriately, this is used for the following purposes:

Members and local variables or parameters for a zone classification

As an argument to invoke a method

http://www.bkjia.com/PHPjc/950334.html www.bkjia.com true http://www.bkjia.com/PHPjc/950334.html techarticle C # Should know the simple point of knowledge 1. Local variables you may be a bit stunned by the title, what is this? See a small example: static void Main () {int a=10; MyClass mc=new M ...

  • Related Article

    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.