2017-9-20c# Notes (classes, access modifiers and accessibility, static members and instance members, fields, main methods, constants, creation of objects, properties of classes)

Source: Internet
Author: User
Tags modifiers

The two main methods of software development are object-oriented and process-oriented, the focus of the process is mainly on the combination and optimization of instruction, the object-oriented focus is transferred from the instruction to the data and function of the organization program.

    1. Class

The data and functions of a program are organized into a packaged collection of logically related data items and functions, called classes. As an integral and indivisible whole, the class hides concrete implementation details from the outside, thus realizing the features of reusable and maintainable, and also provides the inheritance function to support subclasses to obtain the parent class characteristics.

A class is a data structure that can store data and execute code, is a wrapper for logically related data and functions, and usually represents something in the real world or conceptually.

Class includes the following:

(1) Data members: Data members are used to store data related to a class or object. Data members typically emulate the nature of real-world things represented by the class

(2) Function members: function members execute code, usually simulating the functions and operations of real-world things represented by a class.

Members can make any combination of 9 possible members:

A class is a user-defined data type:

The general form of a class is defined in C #:

[class modifier] class name


Member List


A class definition, like a struct definition, does not allocate storage space for it.

    1. The meaning and accessibility of the access modifier:

The use of access modifiers is supplemented by:

(1) All members that are explicitly declared in a class are visible to each other, regardless of the access control properties of those members

(2) Members of a class do not have more open access than the whole class

(3) The access modifier can appear 0 or more times in any order, and an access modifier modifies only the current class member.

(4) If a member does not carry any access modifiers at the time of declaration, it is a private member by default.

    1. Static members and instance members

You can divide a class member into static members and instance members for decoration using the keyword static for a member definition

(1) Instance members:

An instance member is sometimes called a fee static member, and it is related to the object of a class member. When the declarations of fields, methods, properties, events, indexers, destructors, and constructors do not contain the static modifier, it lives as an instance member. Characteristics of Instance Members:

① a reference using the form "class object. Member name"

Each object of the ② class contains a set of all instance fields for that class, respectively

The ③ instance function member acts on the given object of the class and can be accessed with this accessor.

(2) Static members

Static members are related to classes rather than class objects

When a field, method, property, event, operator, or constructor declaration contains a static modifier, it declares a statics member. A constant member of a class implicitly declares a static member

Static members have the following characteristics:

① a reference using the "class name. Member name" form

② the storage of static fields is shared by all objects of the class, only one copy

③ static function members cannot act on specific objects that cannot be accessed using the this accessor.

4. Fields

A field is a variable that belongs to a class. He can be any data type, including predefined types and user-defined types. Fields can be used to hold data and to support read and write operations.

Declaration syntax for a field:

[Static] Access modifier data type field name;

The field of the static modifier is the static field of the class, as long as the class definition allocates space only by the compiler. A field that does not have a static keyword decoration is an instance field of the class, and only the object or class instance is allocated the storage space for that field after the class is defined or instantiated.

The static field of a class has a displacement of storage space and is publicly accessible by all instances of the class. For instance fields of a class, each class instance has a separate storage space to store the instance field.

Variable fields modified with Volitile the use of optimized technical dishes results in a different access result than non-mutable fields, only in a multithreaded program run environment.

5.Main Method

The Main method complements:

(1) The Main method must be declared static or the program cannot start

(2) The main method can be declared in any class or struct in the program.

(3) The main method is declared as private by default, and the current program can only be started by executing the environment, and the main method can be declared as Publi so that other programs can access it.

    1. 1. Constants

Declaration syntax for constants:

Access modifier const data type constant name = constant value

such as const double PI = 3.14;

The value used to initialize a constant member must be computable at compile time, and is usually a predefined simple type or an expression composed of them. A member constant can only assign a value to a constant member when declared, and any action that assigns a value to a constant member after a declaration causes a compilation error.

A constant member does not have its own storage location, but is replaced by the compiler at compile time, so it cannot be decorated with the static keyword.

There are no global variables in C #, and each constant must be declared within the type.

    1. 2. creation of objects

You need to use the new operator when creating an object. The new operator can allocate and initialize memory for an instance of any specified type. The syntax for creating an object is as follows:

Object name = new class name ();

You need to be aware of when creating objects:

(1) The class name of the class to which the object is to be created after the new operator;

(2) The parentheses after the class name must not be omitted

(3) You can combine the definition and creation of objects, such as Point P1=new Point ()

8. Properties of the class

The Access permissions for class fields are generally defined as private and protected, and outside of the class is accessed through the public method of the class.

The properties in C # Combine fields with the methods that access them. property provides class users with the same use of Read and write fields.

Properties are worth reading and writing through the get accessor and the set accessor, and can be checked for illegal assignment.

(1) Definition of attributes

The general form of a property declaration is:

[Access modifier] Property Type property name




Get Property Worth Code

return expression;//You can enter more code here, here is the typical code


set {

Code to set property values



2017-9-20c# Notes (classes, access modifiers and accessibility, static members and instance members, fields, main methods, constants, creation of objects, properties of classes)

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.