首页  >  文章  >  后端开发  >  C# 自动实现的属性

C# 自动实现的属性

WBOY
WBOY原创
2024-09-03 15:21:49671浏览

在类的 get 方法和 set 方法中使用时不需要任何代码的属性在 C# 中称为自动实现属性。使用它,给定的代码变得更加可读和简洁,并且当在代码中使用这些属性时,编译器将创建私有字段,该字段对应于这些属性,并且只能使用 get 方法和 set 方法进行访问。此类自动实现的属性不能在接口中声明,因为接口不允许编译器创建与自动实现的属性对应的私有字段。这些在 C# 3.0 版及更高版本中可用。

语法

C# 自动实现属性的语法如下:

Public data_type property_name{ get; set; }

其中 data_type 是属性的数据类型,

property_name 是属性的名称。

C# 自动实现属性的工作

  • 当需要在 get 方法和 set 方法内部使用一些没有任何代码的属性时,这些属性在 C# 中称为自动实现的属性。
  • 通过使用 C# 中自动实现的属性,代码变得更加简洁和可读。
  • 当程序中使用 Auto 实现的属性时,编译器会创建与这些属性对应的私有字段,只能使用 set 方法和 get 方法访问这些私有字段。
  • 自动实现的属性不能在接口中声明,因为这些属性对应的编译器创建的私有字段(只能使用 set 方法和 get 方法访问)是接口不允许的。
  • 自动实现的属性是在 C# 3.0 版本以及更高版本的 C# 中引入的。

C# 自动实现的属性示例

下面提到了不同的示例:

示例#1

C# 程序,演示程序中自动实现的属性,通过将某些变量设置为自动实现的属性来获取书籍的详细信息,使其只能使用 get 和 set 方法访问。

代码:

using System;
using System.Collections.Generic;
//a namespace called check is defined
namespace Check
{
//a class called books is defined
class Books
{
// three auto implemented properties are defined which can be accessed using set and get method
public int BookID { get; set; }
public string BookName { get; set; }
public string BookAuthor { get; set; }
}
//a class called property is defined
class property
{
//main method is called
public static void Main(string[] args)
{
//an instance of the books class is created
Books books = new Books();
//the auto implemented properties defined before are set with certain values
books.BookID    = 10;
books.BookName  = "To Kill a mocking bird";
books.BookAuthor = "Harper Lee";
// the auto implemented properties defined before are obtained using get method
Console.WriteLine("The BookID of the given book is: {0} ", books.BookID);
Console.WriteLine("The name of the given book is: {0} ", books.BookName);
Console.WriteLine("The Author of the given book is: {0} ", books.BookAuthor);
}
}
}

输出:

C# 自动实现的属性

在上面的程序中,定义了一个名为 check 的命名空间。然后定义一个名为 books 的类。然后定义了三个自动实现的属性,可以使用 set 和 get 方法访问它们。然后定义一个名为property的类。然后创建该书类的一个实例。然后将之前定义的自动实现的属性设置为某些值。然后使用get方法获取之前定义的自动实现的属性。

示例#2

C# 程序,演示程序中自动实现的属性,通过将某些变量设置为自动实现的属性来获取书籍的详细信息,使其只能使用 get 和 set 方法访问。

代码:

using System;
using System.Collections.Generic;
//a namespace called check is defined
namespace Check
{
//a class called players is defined
class players
{
// three auto implemented properties are defined which can be accessed using set and get method
public int playerposition { get; set; }
public string playerName { get; set; }
public string playerteam { get; set; }
}
//a class called property is defined
class property
{
//main method is called
public static void Main(string[] args)
{
//an instance of the books class is created
players play = new players();
//the auto implemented properties defined before are set with certain values
play.playerposition    = 1;
play.playerName  = "Sachin Tendulkar";
play.playerteam = "India";
// the auto implemented properties defined before are obtained using get method
Console.WriteLine("The position  of the given player is: {0} ", play.playerposition);
Console.WriteLine("The name of the given player is: {0} ", play.playerName);
Console.WriteLine("The team for which the player plays is: {0} ", play.playerteam);
}
}
}

输出:

C# 自动实现的属性

在上面的程序中,定义了一个名为 check 的命名空间。然后定义一个名为players的类。然后定义了三个自动实现的属性,可以使用 set 和 get 方法访问它们。然后定义一个名为property的类。然后创建玩家类的实例。然后将之前定义的自动实现的属性设置为某些值。然后使用get方法获取之前定义的自动实现的属性。最后,输出如上面的快照所示。

优点

在 C# 中使用自动实现的属性有几个优点。他们是:

  • 通过使用自动实现的属性,应用程序编程接口可以面向未来。因此,如果我们想稍后在 getter 或 setter 中使用逻辑,应用程序编程接口不会改变。
  • 数据绑定只能通过使用自动实现的属性来实现。这是因为数据绑定框架仅适用于属性,不适用于字段。

结论

在本教程中,我们通过定义、语法和编程示例及其输出和优点来了解 C# 中自动实现属性的概念。

以上是C# 自动实现的属性的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Byte to String C#下一篇:C# Memory Leak