C #, some very simple but should know knowledge points,

Source: Internet
Author: User

C #, some very simple but should know knowledge points,
1. Local Variables

You may get a glimpse of this title. What is this. Let's look at a small example:

Static void main ()
{
Int a = 10;
MyClass mc = new MyClass ();
}

Haha, here a and mc are local variables. They are the same as fields and also store data. Fields usually store data related to the object state, while creating local variables is often used to save local or temporary data. It's easy, but what is the difference between it and the instance field:

 

 

Instance Field

Local variable

Lifetime Starting from instance creation until the instance is no longer accessed Starts when it is declared in the block and ends when the block execution ends.
Implicit Initialization Initialize to the default value of this class No implicit initialization. If the variable is not assigned a value before use, the compiler reports an error.
Storage Area Because the instance field is a member of the class, all the fields are stored in the heap, whether it refers to the type or reference type Value Type: stored in the stack
Reference Type: The reference is stored in the stack, and the data is stored in the heap.

 

2. var keyword

We know that var can automatically infer the type of the variable. In the code above, we can find that the compiler can deduce its type from the right side of the initialization statement when the type name is provided at the beginning of the Declaration. Therefore, at the beginning of the declaration, the display type name is redundant.

To avoid this redundancy, you can use the new keyword var at the position of the display type name at the beginning of the Declaration. The code above can be changed:

Static void main ()
{
Var a = 10;
Var mc = new MyClass ();
}

 

3. Value Parameters

Using Value parameters, data is transmitted to the method by copying the values of real parameters to the form parameters. When the method is called, The system performs the following operations:

    • Allocate space for the form parameter in the stack
    • Copy the value of the real parameter to the form parameter
4. reference parameter (ref)
    • No memory is allocated for the form parameter on the stack
    • The actual situation is that the parameter name of the form parameter will be used as the alias of the real parameter variable, pointing to the same memory location
    • Real parameters must be variables and must be assigned a value before being used as real parameters. If it is a reference type variable, you can assign a reference or null value.
5. output parameters (out)
    • In a method, the output parameter must be assigned a value before it can be used. This means that the initial value of the parameter does not work, so it is not necessary to assign a value to the parameter before the method call.
    • Before returning a method, all output parameters must be assigned a value to any path in the method.
      The following code:

Public void Add (out int outValue) {int var1 = outValue + 2;} an error is returned if the above Code output parameter is read before the method value is assigned.

6. attributes (set, get)
    • Attribute is a function member.
    • It does not allocate memory for data storage.
    • It can execute code
    • The set accessors have a separate and implicit Value parameter named Value, which is of the same type as the property and has a return type void.
    • The get accessors have no parameters and have the same return type as the property type.
7. object initialization statement

We all know that creating an object is composed of a class constructor and Its Parameter List after new.

The object initialization statement extends the creation syntax. A group of initialization statements are placed at the end of the expression. Allow us to set the field and attribute values when creating a new object instance.

This syntax has two forms: one is to include the list of constructor parameters, and the other is not to include. Note that parentheses can be omitted in the first form.

New TypeName {FieldOrProp = InitExpr, FieldOrProp = InitExpr ,...}

New TypeName {FieldOrProp = InitExpr, FieldOrProp = InitExpr ,...}

Example:

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

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

  • Instance Constructor
  • Instance method
  • Instance access permissions for attributes and indexers
    Because static members are not part of an instance, you cannot use the this keyword in the code of any static function member. More appropriately, this is used for the following purposes:
  • Member used for partition classification and local variables or parameters
  • As a real parameter for calling a method

Well, I think most of my shoes can understand the above. These are basic things, and some of them may be useless, I have never noticed such a thing. So I have summarized some information for your reference. I hope you can gain some benefits.

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.