C # Basic Grammar review-Create and manage classes and objects

Source: Internet
Author: User

Control accessibility:

Private a method or field is only allowed to be accessed from within the class, and the Private keyword is the default

Public methods or fields can be accessed from within and outside of the class

The variables declared in the method are not initialized by default, and the fields in the class are automatically initialized to 0,null,false

Using the constructor:

Has the same name as the class. Can get the parameter, but cannot return any value (even void), the constructor is generally declared public, and if declared as private, the object of the externally constructed class can no longer be class.

Partial class:

Class is decorated with the partial keyword.

If the parameter in the method has the same name as a variable, the parameter overrides the field in any statement of the method, and the field is applied with the This keyword before the field.

Understanding static methods and data:

If you declare a method or field as static, you can call a method or field with the class name, in a static static method, you cannot access any instance fields defined in the class, you can only access the static field, and you can call only the other static methods in the class. A non-static method must first create an instance before the methods can be called on the object.

To create a shared field:

When a field is defined as static, you can create a field that can be shared among all objects of the class. Modifying a static field will work for all objects. Call a static field to make a call using the class title as a prefix.

To create a static field using the Const keyword:

The value of a field that is decorated with the Const keyword never changes, and its decorated field can only be an enumeration, a numeric type, or a string.

Static class:

The static keyword is used in the class's modifier, which can only contain static members, static classes cannot contain any instance data and methods, and the static fields and methods cannot append the This keyword as a prefix.

Anonymous class:

Anonymous classes can contain only public fields, fields must all be initialized, cannot be static fields, and no methods can be specified in them. The format is: Var a=new {name= "mountains", age=11};

C # Basic Grammar review-Create and manage classes and objects

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.