Class and Object

Source: Internet
Author: User

The basic types in C # Are all value types created in the stack, while the object is a reference type created in the heap. The new keyword must be used.

Constructor:

The constructor has the same name as the class and no return type.

This Keyword:

Restrict hidden members with similar names, for example:


public Employee(string name, string alias){    // Use this to qualify the fields, name and alias:    this.name = name;    this.alias = alias;}

Passing objects as parameters to other methods

Class myClass{    Public void foo(OtherClass otherObject){otherObject.Bar(this);}}

There is a myclass. Foo method that calls the otherobject. Bar method in the method and passes in a reference to the current myclass instance. In this way, the bar method can use the public method and members of the current myclass instance.

Declare the indexer, for example:

public int this[int param]{    get { return array[param]; }    set { array[param] = value; }}

Because static member functions exist at the class level and are not part of an object, there is no this pointer. It is wrong to reference this in a static method.

Call another one from the overload constructor:

Class myClass{    Public myCall (int i){//}    Public myCall ():this(42){//}}
In this example, the default constructor uses the this keyword to call the overload constructor whose parameter is an integer.

Static members:

Non-static classes can contain static methods, fields, attributes, or events. You can call static members in the class even if no instance of the class is created. You can always access static members by class name instead of Instance name. No matter how many instances are created for a class, its static members have only one copy. Static methods and attributes cannot access non-static fields and events in their contained types, and cannot access instance variables of any objects (unless explicitly transmitted in method parameters ).

A more common approach is to declare a non-static class with some static members, rather than declaring the entire class as a static class. Static fields have two common usage methods: one is to record the number of instantiated objects, and the other is to store values that must be shared among all instances.

Static methods can be overloaded but cannot be overwritten because they belong to a class and do not belong to any instance of the class.

Although the field cannot be declared as static const, the behavior of the const field is static in nature. Such fields belong to the type and do not belong to the type instance. Therefore, you can use classname. membername notation to access the const field like static fields. No object instance is required.

C # does not support static local variables (variables declared within the method range ).


Instances

public class Cat{    public Cat()    {        instances++;    }    public static void HowManyCats()    {        Console.WriteLine("{0} cats adopted",              instances);    }    private static int instances = 0;}public class Tester{    static void Main()    {        Cat.HowManyCats();        Cat frisky = new Cat();        Cat.HowManyCats();        Cat whiskers = new Cat();        Cat.HowManyCats();        Console.ReadLine();    }}
If a static constructor is declared, you must ensure that the static constructor runs before the instance is created. No access modifier before the static constructor and non-static member variables cannot be accessed.


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.