C# 自动实现的属性
在类的 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); } } }
输出:
在上面的程序中,定义了一个名为 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); } } }
输出:
在上面的程序中,定义了一个名为 check 的命名空间。然后定义一个名为players的类。然后定义了三个自动实现的属性,可以使用 set 和 get 方法访问它们。然后定义一个名为property的类。然后创建玩家类的实例。然后将之前定义的自动实现的属性设置为某些值。然后使用get方法获取之前定义的自动实现的属性。最后,输出如上面的快照所示。
优点
在 C# 中使用自动实现的属性有几个优点。他们是:
- 通过使用自动实现的属性,应用程序编程接口可以面向未来。因此,如果我们想稍后在 getter 或 setter 中使用逻辑,应用程序编程接口不会改变。
- 数据绑定只能通过使用自动实现的属性来实现。这是因为数据绑定框架仅适用于属性,不适用于字段。
结论
在本教程中,我们通过定义、语法和编程示例及其输出和优点来了解 C# 中自动实现属性的概念。
以上是C# 自动实现的属性的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

使用 C# 的 Active Directory 指南。在这里,我们讨论 Active Directory 在 C# 中的介绍和工作原理以及语法和示例。

多线程和异步的区别在于,多线程同时执行多个线程,而异步在不阻塞当前线程的情况下执行操作。多线程用于计算密集型任务,而异步用于用户交互操作。多线程的优势是提高计算性能,异步的优势是不阻塞 UI 线程。选择多线程还是异步取决于任务性质:计算密集型任务使用多线程,与外部资源交互且需要保持 UI 响应的任务使用异步。

可以采用多种方法修改 XML 格式:使用文本编辑器(如 Notepad )进行手工编辑;使用在线或桌面 XML 格式化工具(如 XMLbeautifier)进行自动格式化;使用 XML 转换工具(如 XSLT)定义转换规则;或者使用编程语言(如 Python)进行解析和操作。修改时需谨慎,并备份原始文件。
