Class knowledge in C #

Source: Internet
Author: User

NClass
Class is object-orientedProgramThe core concept in the Design. A class usually represents a set of object operations.
Definition method:
Modifier Class Name
{
....
}
Common modifiers:
Public: unrestricted access
Protected: access to a derived class that only contains a class or this class
Internal: access is limited to the current Assembly (usually under the same namespace)
Protected internal: contains only the current Assembly or type derived from the class.
PRIVATE: access is limited to include types only.

The class contains the following content (members ):

Field: that is, the variable in the class that stores the content. The default access is public.
Example:Privaet string color1

Method: indicates an action of the class.
Example:Public void run ()
{
.....
}
Void indicates no return value. If there is a return value, void should be changed to the returned type class, such as int

Constructor:
A constructor is a method with the same name and class. It initializes the values of field variables and attributes.

Method overload:
Method overloading refers to different processing methods of the same method. The difference is that the parameter types in the method are different.
For example, public int test1 (string AAA, int BBB) is a method, while public int test1 (int bbb, string AAA) is another method, whether the parameter is reloaded depends on the parameter type comparison in order, rather than the parameter name.

Class attributes: attributes are the self-identification of external classes. The common method is as follows:
Public String color
{
Get
{
Return color;
}
Set
{
Color = value;
}
}

The above defines a color attribute. The get method can be used for full read, and the set method can be used to assign values.
Generally, the operations for class attributes and modifying to public fields are basically the same, but attributes have more flexible control than fields, such as defining whether to read or write data, in addition, you can perform computation or other operations in the get or set method. fields can only be assigned and read.

Class indexer: the class indexer is a special attribute that allows the class structure or instance to be indexed in the same way as the array.
Generally, variables or attributes of the array type can be used.
Definition method: It is basically the same as the attribute, but only has a keyword: This []. Note that the index value can be not only a value, but also a string.
Class car
{
String [] Wheels = new string [4];

Public Car ()
{
Wheels [0] = "Left front wheel ";
Wheels [1] = "front right wheel ";
Wheels [2] = "Left rear wheel ";
Wheels [3] = "right rear wheel ";
}

Public String This [int Index] // car-class Indexer
{
Get
{
Return wheels [Index];
}
Set
{
Wheels [Index] = value;
}
}
}

Class inheritance: the class can inherit other classes, so that the inherited class has the characteristics of other classes.
Method: class derived class: access modifier base class
{

}

Class encapsulation: encapsulation mainly prevents unauthorized access to some information and functions by the outside world.

Class polymorphism: polymorphism is based on inheritance.CodeMore common, less specific

Class interface: it is also a method for implementing Polymorphism
Example:Public interface test1 ()
{

}
An interface is equivalent to an empty box of a class. It must be implemented when derived from it. The interface can have attributes, methods, and indexers.

Class delegate: The delegate is designed to implement the callback function. It is a data structure.
Definition:Public Delegate string test1 (string AAA)
It is important to delegate its parameters and return values.

Class events: combined with the delegate to implement some response functions, such as clicking the mouse.

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.