简体   繁体   English

有没有一种更快的方法来声明多个{get; set;}属性在C#中?

[英]Is there a quicker way to declare multiple {get; set;} properties in c#?

I need to make multiple properties accessible from within other methods, I'm aware that I can do it by using: 我需要从其他方法中访问多个属性,我知道我可以使用以下方法做到这一点:

int january {get; set;};
int february {get; set;};

I need to do this for every month for multiple other properties. 对于其他多个属性,我每个月都需要这样做。 My question is if there is a quicker way to do this, just like it's possible to declare normal variables like so: 我的问题是,是否有更快的方法可以执行此操作,就像可以这样声明普通变量一样:

int january, february, march, april;

You can use prop(And then press tab) to auto-write type and name: 您可以使用prop(然后按Tab键)自动写入类型和名称:

public TYPE Type { get; set; }

Which VS will then ask you to replace the 'TYPE' and name ('Type') of the variable. 然后哪个VS会要求您替换变量的“ TYPE”和名称(“ Type”)。

No you can't do this in a quicker way. 不,您不能以更快的方式执行此操作。 Basically the following 基本上如下

int january {get; set;};

when would be compiled a private backing field would be created and two methods, one for setting it's value and one for retrieving it would be created. 当将其编译时,将创建一个私有支持字段,并创建两种方法,一种用于设置其值,另一种用于检索它。 So it's impossible use this syntax as the following: 因此,不可能将这种语法用于以下情况:

int january, february, march, april;

which just declares that all these variables are of type int . 它只是声明所有这些变量都是int类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM