1024programmer Asp.Net Properties in C#

Properties in C#

Properties in C#

The public account “DotNet Learning Exchange” shares the details of learning DotNet. This article briefly introduces properties in C#.

The public account “DotNet Learning Exchange” shares the details of learning DotNet.

Introduction

A property is a member that represents an instance of a class or a data item in a class. Using properties looks very much like writing or To read a field, the syntax is the same.

The following code shows the use of a class named D. It has a public field and a public property. From the usage Can’t tell them apart.

D d = new D();

d.MyField = 7; //Assign a value to the field
d.MyProperty = 10; //Assign a value to the attribute
Console.WriteLine($"{d.MyField} {d.MyProperty}"); //Read fields and attributes

Similar to fields, attributes have the following characteristics:

1. It is a named class member.

2. It has types.

3. It can be assigned and read.

Unlike fields, attributes are A function member.

1. It does not allocate memory for data storage.

2. It executes code.

A simple example about attributes

The code is as follows:

public class Person
{
private string name; // Private field

public string Name
{
                                                                                              > {
// This is the get accessor of the attribute, used to get the value of the attribute
return name;
} }
                                                                                       // This is the set accessor of the attribute, used to set the value of the attribute
                                                                                                    operator">= value;
} }
}
}

The illustration for this example is as follows:

image-20231108083339273

In this example, Name The attribute provides access to the name field, but it It does not store data itself. When you use If you don’t want to define an accessor for a property, you can ignore the declaration of the accessor.

Properties with only get accessors are called Read-only attribute. Read-only properties are a way to safely pass an item of data out of a class or instance of a class without allowing too many access methods.

Properties with only set accessors are called Write-only attributes. Write-only properties are a safe way to pass a piece of data into a class from outside the class without allowing too much access.

At least one of the two accessors must be defined, otherwise the compiler will report an error, as shown in the following figure:

image-20231108090006210

Auto-implemented properties

Because properties are often associated with backing fields, C# provides Automatically implemented property, allows only to declare properties without declaring backing fields , the compiler will create hidden backing fields for us and automatically hook them to the get and set accessors.

Notes on automatically implementing attributes:

1. If the backing field is not declared, the compiler will allocate storage according to the type of the attribute.

2. Accessor method bodies cannot be provided, they must be simply declared as semicolons. get is responsible for simple memory reading, and set is responsible for simple writing.

3. The backing field can only be accessed through the accessor. Since it cannot be accessed in any other way, there is no point in implementing read-only and write-only properties, so both read and write accessors must be provided.

An example of automatically implementing attributes, the code is as follows:

 public class Person
{
public string Name // No backing field declared
{
get; set; // The method body of the accessor is declared as a semicolon
}


}
internal class Program
{
static void Main(string[] args)
{
                                                                            ="cm-variable">person = new Person();
Console.WriteLine(person.Name); // Use automatic attributes like rule attributes
                                                                                      class="cm-operator">= "小王";
           Console.WriteLine(person.Name);
}
}

The running results are as shown below:

image-20231108093147756

Reference materials

“C# Illustrated Tutorial (4th Edition)” – Daniel M. Solis

As shown in the picture below:

image-20231108093147756

Reference materials

“C# Illustrated Tutorial (4th Edition)” – Daniel M. Solis

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/properties-in-c/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索