C # Get Set Function Analysis

Source: Internet
Author: User

Reprinted: http://kb.cnblogs.com/page/50502/

C # Get Set functions are very commonly used, but it still requires a lot of experience to use them. The following article will help you accumulate C # Get Set function experience.

C # Get set does not advocate setting the protection level of the domain to public so that users can perform any operation outside the class-that is too non-oo, or the specific point is too insecure!For all fields that need to be visible outside the class, C # is recommended to use attributes for representation.The attribute does not represent the storage location, which is the fundamental difference between the attribute and the domain. The following is a typical attribute design:

 

using System;  
class MyClass
{
int integer;
publicint Integer
{
get {return integer;}
set {integer=value;}
}
}
class Test
{
publicstaticvoid Main()
{
MyClass MyObject=new MyClass();
Console.Write(MyObject.Integer);
MyObject.Integer++;
Console.Write(MyObject.Integer);
}
}

As we expect, the program outputs 0 1. We can see that attributes provide a friendly access interface for domain members to programmers through method packaging. Here, value is the key word of C # Get set. It is the implicit parameter of set when we perform attribute operations, that is, the right value when we perform attribute write operations.

Attribute provides read-only (get), write-only (SET), read-write (get and set) Three interface operations. The three operations on the domain must be declared under the same attribute, but they cannot be separated. See the following implementation:

 

class MyClass  
{
privatestring name;
publicstring Name
{
get
{
return name;
}
}
publicstring Name
{
set { name = value; }
}
}

The preceding method for separating the name attribute is incorrect! We should put them together like in the previous example. It is worth noting that three attributes (read-only, write-only, read-write) are considered by C # Get set as the same attribute name. See the following example:

 

Class myclass
{
Protectedint num = 0;
Publicint num
{
Set
{
Num = value;
}
}
}
Class myclassderived: myclass
{
Newpublicint num
{
Get
{
Return num;
}
}
}
Class Test
{
Publicstaticvoid main ()
{
Myclassderived myobject = new myclassderived ();
// Myobject. num = 1; // error!
(Myclass) myobject). num = 1;
}
}

Because of the nature of the attribute method, attributes also have various methods for modification. There are also five types of access modifiers for attributes, but the access modifier for attributes is usually public. Otherwise, we will lose the meaning of attributes as the public interface of the class. In addition to the non-existent feature attributes such as method overloading caused by multiple parameters of methods, virtual, sealed, override, and abstract modifiers perform the same behavior on attributes and methods, however, because attributes are implemented as two methods in essence, we need to pay attention to some of its behaviors. See the following example:

 

abstractclass A  
{
int y;
publicvirtualint X
{
get
{ return0; }
}
publicvirtualint Y
{
get { return y; }
set { y = value; }
}
publicabstractint Z { get; set; }
}
class B: A
{
int z;
publicoverrideint X
{
get { returnbase.X +1; }
}
publicoverrideint Y
{
set
{
base.Y = value <0?0: value;
}
}
publicoverrideint Z
{
get
{
return z;
}
set
{
z = value;
}
}
}

This example focuses on some typical behaviors of attributes in the inheritance context. Here, Class A must be declared as abstract because of the existence of abstract attribute Z. In subclass B, the attribute of parent class A is referenced by the base keyword. In Class B, the virtual attribute in Class A can be overwritten only through Y-set.

Static attributes, like static methods, can only access static domain variables of the class. We can also declare external attributes like external methods. The above is a brief introduction to C # Get set.

Related Article

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.