. NET Foundation Review (II)

Source: Internet
Author: User
Tags instance method

A. Structure

    1. A struct is a value type.
    2. Use the struct keyword to define a structure in which the level is consistent with the class and written under the namespace.
    3. You can define fields, properties, methods, constructors, or create objects from the New keyword.
    4. A field in a struct cannot have an initial value assigned to it.
    5. Parameterless constructors No matter what the C # compiler will generate, we cannot define an parameterless constructor for the struct.
    6. The re-constructor must assign values to all the fields of the struct.
    7. The property is assigned a value in the constructor, and is not considered to be a field assignment because the property is not necessarily a field to manipulate.
    8. Automatic attributes cannot be defined because automatic attributes generate a field that must be required in the constructor, but we do not know what the name of the field is.
    9. When we want to represent a lightweight object, we can define the structure to increase the speed. Depending on the influence of the value of the transfer, you want the reference to be defined as a class, and you want to wear a copy as a structure.

Two Garbage collection mechanism

    1. Managed code and unmanaged code. The CLR-managed code is managed code, a variable allocated in the stack space that is immediately reclaimed by the CLR as soon as it executes its scope.
    2. Value type variables are defined as structs, allocating re-stack space, and reference-type variables are defined as classes that are allocated in heap space.
    3. The object allocated in the heap, when no variables refer to him, the object is marked as "junk", waiting for the garbage collector to recycle (garbage Collection), the GC will periodically clean up garbage objects in the heap space.
    4. Frequency of garbage cleanup by GC programmers cannot decide that the CLR will automatically control code performance based on Computer configuration.
    5. GC code calls (the concept of "generation"):

Gc. Getgeneration (P); Gets the generation of the specified object;

Gc. Maxgeneration; return algebra;

Gc. Collection (); Immediately allow the garbage collector to recycle all generations;

Gc. Collection (int); Immediate garbage collection for the specified generation;

Three Destructors

Example:

~person ()

{

Console.WriteLine ("I am a destructor!") ”);

}

    1. Cannot have access modifiers, cannot have parameters;
    2. When the object is reclaimed by the garbage collector, the destructor is automatically called by the GC.
    3. Destructors are used when performing cleanup operations.

Four Static members

    1. Static members are decorated by the static keyword and are part of the class, accessed through the class name. Static members.

Instance members are not decorated with the static keyword and are objects that are accessed through the object name. Instance members.

2. When a class is loaded, all static members under this class will be loaded.

3. Static members are created only once, so static members are only one copy of the entire program, and for example members, there are many copies of the object.

4. Static members are created in the static store, and once created, they are not recycled until the program exits. Static members can have an impact on the efficiency of the program to some extent.

5. Variables need to be shared, and these members can be defined as static members when they need to be called frequently throughout the program.

6. In a static method, an instance member cannot be called directly because the object may not exist when the static method is called.

The 7.this/base keyword cannot be used in a static method because it is possible that the object does not already exist.

8. Static members can be called in an instance method, because static members are definitely present at this time.

Static members and instance members are compared:

(1) Life cycle;

(2) storage location;

Five Static class

    1. The class that is decorated with the static keyword.
    2. Only static members can be declared in a static class.
    3. You cannot have an instance constructor in a static class.
    4. A static class cannot be instantiated because there is no instance member, and instantiation is meaningless.
    5. Static classes cannot be inherited either.
    6. The essence of a static class is an abstract sealed class.
    7. If all members under a class need to be shared, the class is defined as a static class.
    8. You cannot declare a variable of a static type.

Six Static constructors

    1. Non-static classes can have static constructors
    2. When a member of a class is accessed for the first time, a static constructor is executed, executed only once, and no static constructor is executed, no matter if it is called again later.

Examples of programs:

1  Public class Person2     {3         Static  Public intnum;4          PublicPerson ()5         {6num=1;7Console.WriteLine ("Instance Constructors");8         }9         StaticPerson ()Ten         { Onenum=2; AConsole.WriteLine ("Static Constructors"); -         } - } the Static voidMain (string[] args) -         { -Console.WriteLine (Person.num);//Output: 2 -Person p =NewPerson (); +Console.WriteLine (Person.num);//Output: 1 - Console.readkey (); +}

Seven Inherited

    1. When you create a subclass object, the sub-class object will open up space for the fields of the subclass object, as well as all the fields of the parent class object.
    2. Subclasses inherit all non-private members of the parent class from the parent class.
    3. The base keyword can call a non-private member of the parent class, and the parameterless constructor of the parent class can be called by: base ().
    4. The access level of a subclass cannot be higher than the parent class.
    5. When a subclass object is created, the constructor of the parent class is called, and if the parent does not have a parameterless constructor, the subclass will error because the subclass object calls the parent class's parameterless constructor by default.
    6. By: Base () can be displayed to invoke the parent class's parameter constructor.

Examples of programs:

1  Public Student ():base("Jack",true)2{3     Console.WriteLine ("I am a sub-class constructor ..."); 4 }

7. When calling subclasses, to prevent data conflicts, the data in the parent class is processed first, and the parent data is initialized.

8. Characteristics of Inheritance:

(1) Single, subclass can have only one parent class;

(2) transitive nature;

9.Object is the ancestor class of all classes, all of which inherit from the object class.

10. Under what circumstances will inheritance be used?

The parent class should not inherit as long as a function is not required by the subclass, and the subclass and parent class are the relationships of is a. For example, a dog can walk, and people will walk, but the person class cannot inherit from the dog class, whereas the student class can inherit the man class.

11. When a subclass member exists with the same name as the parent class member?

When the parent class has the same name as the subclass, if a subclass object is created, calling the method with the same name as the child class object will call the subclass.

The second effect of the 12.new keyword, which hides a member of the same name as the parent class.

Examples of programs:

Parent class:

1 class  Person 2 {3     void Test () 4 {5    Console.WriteLine ("I am the parent class"); 6 }7 }

Sub-class:

1 class Student:person 2 {3public newvoid  Test ()4{5      Console.WriteLine ("I am a sub-category"); 6 }7 }

. NET Foundation Review (II)

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.