Classes in C #: Fields, Properties, methods

Source: Internet
Author: User

There are three types of content in a C + + class: member variables, member functions, and friends.

In general, member variables describe the properties of a class, member functions describe the operation of a class, a friend is a special declaration, a function or class declared as a friend can manipulate its own private member variables and member methods.


In Java, there are two classes of members of a class: properties, which are equivalent to C + + member variables, and methods that are equivalent to C + + member functions.


In C #, there are four members of the class: Fields, Properties, methods, and events (the event has not yet been learned, pause explaining 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0057.gif "alt=" j_ 0057.gif "/>).

A field is equivalent to a member variable in C + + and a property in Java, a variable defined in a class (which can be a primitive type or a reference type), and generally, a field is defined as private.

A method is equivalent to a member function in C + + and a method in Java, which is a function defined within a class.


One of the new things: properties

In Java, Java, because the properties of a class are generally defined as private and cannot be manipulated outside the class, you must provide the appropriate function to set or get the value of the private property. In general, the method that sets the property value begins with set, and the method that gets the value of the property begins with get.

While it is possible to manipulate private fields in C # in the same way, a new feature is provided in C # to achieve this, which is the property.

First, consider the definition of the attribute:

Access modifier Type property name

{

Get

{

Code snippet

}

Set

{

Code snippet

}

}

Formally, the definition of a property is much like the definition of a method, except that it has no parameters.

Looking at the use of properties, the use of properties is much like using a common field.

You can assign a value to a property, or you can use the value of the property directly. For example:

Class myclass{public int MyInt//common property named MyInt {get {//code snippet} set {//Code snippet}}}

In a program, you can use this property

MyClass C = new MyClass (); c.myint = 100; Console.WriteLine (C.myint);

Isn't it much like using a field?


Classes in C #: Fields, Properties, methods

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.