C # Tutorial Lesson Tenth: Properties

Source: Internet
Author: User
Tags access properties constructor implement new features first row
This section of the tutorial describes the properties of C #, which are designed to include:
1. Understanding what attributes are

2. How to Implement Properties

3. Create a read-only property

4. Create a write-only property

Properties are new features that are unique in C #. Properties are used to read and write domains in a class, which has a certain protective function. In other languages, this is accomplished by implementing a specific getter and setter method. C # 's properties are protected, allowing you to access properties just as you would access a domain. To understand the use of attributes, let's look at how to encapsulate the domain in a traditional way.

1. Listing 10-1. Example of a traditional access domain for a class: Accessors.cs

Using System;
public class Propertyholder
{
private int someproperty = 0;
public int Getsomeproperty ()
{
return someproperty;
}
public void Setsomeproperty (int propvalue)
{
Someproperty = PropValue;
}
}

public class Propertytester
{
public static int Main (string[] args)
{
Propertyholder prophold = new Propertyholder ();
Prophold.setsomeproperty (5);
Console.WriteLine ("Property Value: {0}", Prophold.getsomeproperty ());
return 0;
}
}

Description

1. Listing 10-1 shows an example of accessing a class's domain using traditional methods.

The Propertyholder class has a domain someproperty that we are interested in, Propertyholder class with two methods: Getsomeproperty and Setsomeproperty. The Getsomeproperty method returns the value of the Someproperty field. The Setsomeproperty method sets the value of the field Someproperty.

2. Class Propertytester uses the methods in class Propertyholder to get the value of the Someproperty field.

A Propertyholder object is newly created in the main method, and then the Setsomeproperty method of the Prophold object is called by using the Setsomeproperty method, setting its value to 5. The Console.WriteLine method is then invoked to output the property value. A getsomeproperty call to the Prophold object that is used to get the property value. It outputs "property value:5" to the console.

3. This traditional approach to accessing domain information is good because it supports the concept of object-oriented encapsulation.

The above method still applies if the type of the field changes from the int type to the byte type in the implementation of the domain Someproperty. Now, if you take attributes, the implementation will be smoother.

2. Listing 10-2. To access the domain of a class using attributes: Properties.cs

Using System;
public class Propertyholder
{
private int someproperty = 0;
public int Someproperty
{
Get
{
return someproperty;
}
Set
{
Someproperty = value;
}
}
}

public class Propertytester
{
public static int Main (string[] args)
{
Propertyholder prophold = new Propertyholder ();
Prophold.someproperty = 5;
Console.WriteLine ("Property Value: {0}", Prophold.someproperty);
return 0;
}
}

Description

1. Listing 10-2 shows how to create and use attributes.

There is an implementation of the "Someproperty" attribute in the Propertyholder class. Note: The first letter of the property name must be uppercase, which is the only difference between the property name "Someproperty" and the domain name "someproperty". Properties have two types of access operations: get and set. The Get access operation returns the value of the Someproperty field. A set access operation is to set the value of the Someproperty field, whose value is the content of "value". The "value" following the set access symbol is a reserved word in C #. In general, there will be an error using the "value" keyword in other situations.

The 2.PropertyTester class uses the Someproperty property in the Propertyholder class.

In the first row of the Main method, a Propertyholder object prophold is created. After that, set the value of the Someproperty field of the Prophold object to 5, very simply, like assigning values to a field, assigning a value to a property.

The 3.console.writeline method outputs the value of the Someproperty field of the Prophold object.

This is done by using the Someproperty property of the Prophold object. It's simple, just like assigning a field to a property. Property can be set to read-only, which can be achieved by simply setting a get access symbol in the property's implementation.

3. Listing 10-3. Read-only properties: ReadOnlyProperty.cs

Using System;
public class Propertyholder
{
private int someproperty = 0;
Public propertyholder (int propval)
{
Someproperty = PropVal;
}
public int Someproperty
{
Get
{
return someproperty;
}
}
}

public class Propertytester
{
public static int Main (string[] args)
{
Propertyholder prophold = new Propertyholder (5);
Console.WriteLine ("Property Value: {0}", Prophold.someproperty);
return 0;
}
}

Description

1. Listing 10-3 shows how to implement a read-only property.

In the Propertyholder class, the Someproperty property has only one get access operation, and no set access operation is used. There is also a constructor in the Propertyholder class that accepts an integer parameter.

2. In the main method of the Propertytester class, the object of the new Propertyholder class named Prophold is created.

The Prophold object invokes the Propertyholder constructor with arguments when it is instantiated. In this case, the parameter value is 5, which initializes the value of the Someproperty field of the Prophold object.

3. Because the Someproperty property of the Propertyholder class is read-only, there is no other way to set the value of the Someproperty field.

If you insert a "Prophold.someproperty = 7" statement into the list of programs, the program compilation will not pass because Someproperty is a read-only property. When the Someproperty property is used in the Console.WriteLine method, the program executes normally. This is because the method invokes the get access operation of the Someproperty property, which is a read-only operation.

4. Listing 10-4. Write-only properties: WriteOnlyProperty.cs

Using System;
public class Propertyholder
{
private int someproperty = 0;
public int Someproperty
{
Set
{
Someproperty = value;
Console.WriteLine ("Someproperty is equal to {0}", Someproperty);
}
}
}

public class Propertytester
{
public static int Main (string[] args)
{
Propertyholder prophold = new Propertyholder ();
Prophold.someproperty = 5;
return 0;
}
}

Description

1. Listing 10-4 shows how to create and use write-only properties.

This time, in the Someproperty attribute in the Propertyholder class, the Get access operation is removed and the set access operation is added. The function is to output the value of the Someproperty field.

2. In the main method in the Propertytester class, initialize the Propertytester class with the default constructor.

Then, using the Someproperty property of the Prophold object, set the value of the field to 5. This invokes the set access operation of the Prophold object, sets the value of the Someproperty field to 5 and, finally, outputs the "Someproperty is equal to 5" information to the console.

Summary
Now that you know what attributes are and how they are used, you know the difference between using attributes and methods that use traditional classes. Attributes can be read-only or write-only, and you'll know what to use in each case.



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.