两种类型的get,set属性之间有什么区别? [英] What is difference between two types of get ,set properties ?

查看:171
本文介绍了两种类型的get,set属性之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经在C#中看到两种类型的get和set属性,它们之间有什么区别?


i have seen two types of get and set properties in C#, what is the difference between them?

public int setX {get;set;}







and

public int setX
   {
       get
       {
           return y;
       }
       set
       {
           y = value;
       }
   }





Thanks.

推荐答案

在第一个版本中,后备存储由编译器自动生成.基本上可以节省您的键入时间,并为您提供更简洁的代码.

此处的更多信息:自动实现的属性(C#编程指南) [
In the first version, the backing store is auto-generated by the compiler. Basically saves you some typing and gives you cleaner leaner code.

More info here: Auto-Implemented Properties (C# Programming Guide)[^]


----------

Adding info based on the comments: The private field is generated at compile time, so your code cannot access it. If you need to access it then you need to implement a regular property with a manually implemented backing field.


这篇关于两种类型的get,set属性之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆