Class member in C #

Source: Internet
Author: User

The class in C # has fields, attributes, methods, and other class members. This article mainly introduces these class members and the concept of class libraries.

 

  1. Define class members

All members have their own access levels: public, protected, private, and internal. The differences are not mentioned. I don't know Baidu.

Note that protected and internal can be used together to indicate that they can be accessed in the inheritance class of the same project (more accurately, the Assembly.

If static is added before a member, the member is a static member.

  

The difference between a field and a variable: The data member in the class is a field, and he uses the access modifier and the data type class definition (Public string name;). The field is like a small database of the class, it is used to store class-related data. A simple variable has no modifier (INT age;). It cannot be defined directly in the class and can only be defined in the function, it is used as a temporary variable of the method.

 

Define Fields

class Class
{
public int MyInt;
}

Note that pascalcasing is recommended for public fields in the class, while private fields are not recommended, but camelcasing is generally used.

  

If you use the readonly keyword, you can only assign values during the execution of the constructor, or assign values in the initialization assignment statement.

class Class
{
public readonly int MyInt = 2;
}

 

The static keyword is a static field.

class Class
{
public static int MyInt;
}

Reference static variables do not need to be instantiated and can be called directly, such as class. Myint.

 

Definition Method

Use the standard function format.

class MyClass
{
public string MyString()
{
return "Here is a string";
}
}

Note that the public methods (such as fields) in the class are named by pascalcasing.

 

You can also use the static keyword to declare a method. You can also use the following keywords, such:

  

 

  Define attributes

An attribute has two function-like blocks. One is used to obtain the attribute value and the other is used to set the attribute value. These two blocks are also used as accessors to obtain and set the attribute values with the get and set keywords respectively. You can also ignore one of the blocks to create read-only or write-only attributes (ignore get to create write-only attributes, and ignore set to create read-only attributes ). For example:

private int myInt;

public int MyIntProp
{
get { return myInt; }
set { myInt = value; }
}

This simple attribute can only access the Myint field, but we may need more control over the operation in most cases. The following code example:

set 
{
if (value >= 0 && value <= 10)
{
myInt = value;
}
}

If the value assigned to an attribute ranges from 0 to 10, the attribute value is modified. If not, how should I choose? There are four options:

(1) Do nothing: Just like the example above.

(2) Assign the default value to the field;

(3) continue to execute the task. If no error occurs, record the change event for future analysis.

(4) throw an exception.

Generally, the last two options are better. If an exception is thrown:

set 
{
if (value >= 0 && value <= 10)
{
myInt = value;
}
else
{
throw (new ArgumentOutOfRangeException("MyIntProp", value, "MyIntProp must be assigned a value between 0 and 10."));
}
}

 

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.