Use of Get and set, get{} and set{} functions in C #

Source: Internet
Author: User

A:

In C #, get is an action that is made when the property is read, and set is the action that is made when the property is set.

Defines a property that is read-only if it has only get. Similarly, only set, attributes are write-only, and of course only the attributes that are written are not mission-meaningful.

Get and set: Allows read and write access to attributes.
Get only: values that allow only read properties.

Set only: Values for write properties are allowed only.

Get: Performing a Get property is equivalent to reading a field's value.

The following is a get accessor that returns the value of the private field name:

private string name; The Name field

public string name//The Name property

{

Get

{

return name;

}

}

When a property is referenced, the get accessor is invoked to read the value of the property unless the property is an assignment target. For example: Employee e1 = new Employee (); ... Console.Write (E1. Name);

The get accessor must be terminated in a return or throw statement, and the control cannot exceed the accessor body.


The set accessor is similar to the method that returns void. It uses an implicit parameter, called value, whose type is the type of the property.

In the following example, the set accessor is added to the Name property:

public string Name

{

Get

{

return name;

}

Set

{

name = value;

}

}

When you assign a value to a property, the set accessor is invoked with an argument that provides a new value. For example: E1. Name = "Joe";

Notes

Attributes are categorized according to the accessors used, as follows: Properties with only a get accessor are called read-only properties. Cannot assign a value to a read-only property (get).
A property with a set accessor is called a write-only property. Write-only properties cannot be referenced except as targets for assignment.
Properties with both get and set accessors are read-write properties.
In a property declaration, both the get and set accessors must be declared inside the property body.

Example 1 This example shows how to access a property that is hidden by another property in the derived class that has the same name in the base class.

public class BaseClass

{

private string name;

public string Name

{

Get

{

return name;

}

Set
{

name = value;

}

}

}

public class Derivedclass:baseclass

{

private string name;

Public new string Name//Notice The new modifier, the property name in the derived class hides the property name in the base class, and the keyword n ew..

{

Get

{

return name;

}

Set

{

name = value;

}

}

}

public class MainClass

{

public static void Main ()

{

DerivedClass D1 = new DerivedClass ();

D1.                                                                       Name = "John"; Derived Class Property

Console.WriteLine (' Name in the ' derived class is: {0} ', D1. Name);

((BaseClass) d1).                                            Name = "Mary"; Base Class Property

Console.WriteLine (' Name in ' base class is: {0} ')

((BaseClass) d1).                       Name); //conversion (BaseClass) to access hidden properties in the base class

}

}

Output:

Name in the derived class Is:john

Name in the base class Is:mary


The purpose of the set and get is to manipulate the variables inside the class. Instead of directly manipulating the variables of the class.

There is a big role: easy to maintain.


B:

Two functions in C #--A fetch function (get), an assignment function (set), which defines a property

Example://define Attribute Class

Class Person {

private string no= "";

private string Name= "";

public string Userno

{

Get{return NO;} Used to take a value

Set{no=value;} For assignment to-----------private Set{no=value;}

}

public string UserName

{

Get{return Name;}

Set{name=value;} instead-----------private set{name=value;}

}

}

Person Per=new person (); To create an Entity object

Per. userno=10; Per.               Username= "John"; Assigning values to a property

The benefit of this usage is the benefits of this usage benefits of this usage: protecting the privacy of the person class and security

Change the operation of the entity object can not be assigned to Userno and username operation, reduce the assignment of variables, security must be improved

There are many more examples of such writing: Database access,

Public SqlConnection

Getconn {

get {return conn;}

Private set {Conn=value;}

}

By Getconn This property to control all the database objects, you can only access, cannot modify, so it is not easy to error.






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.