简体   繁体   English

通过变量快速创建C#属性

[英]Quick create C# properties from variables

For C#, I hate writing out the variables and then writing out all the properties. 对于C#,我讨厌写出变量,然后写出所有属性。 Isn't there a way to select all variables, right click and create all the properties. 没有办法选择所有变量,右键单击并创建所有属性。

Right click on the field declaration, menu Refactor -> Encapsulate field and you go from 右键单击字段声明,然后单击菜单“重构->封装字段”,然后从

int n;

to

int n;

public int N
{
   get { return n; }
   set { n = value; }
}

If you're using C# 3.0 or above (VisualStudio 2008, essentially), you can use auto properties. 如果您使用的是C#3.0或更高版本(本质上是Visual Studio 2008),则可以使用自动属性。 While this isn't exactly what you're asking for, it should (hopefully) do the trick. 尽管这并不是您所要的,但它应该(希望)达到目的。

Rather than writing: 而不是写:

private string m_Name;

public string Name
{
    get { return m_Name; }
    set { m_Name = value; }
}

You can just write: 您可以这样写:

public string Name { get; set; }

This will give you quick, "dumb" (ie no retrieval or assignment logic) properties that can go on your class. 这将为您提供可以在您的课程上使用的快速,“哑”(即没有检索或分配逻辑)属性。 If you find you need retrieval and assignment logic later, just come back and do the full property declaration syntax and you won't have to change any of the calling code. 如果您发现以后需要检索和赋值逻辑,只需返回并执行完整的属性声明语法,就不必更改任何调用代码。

The only real difference is that you'll have to use the property to get the value within your class, as the backing variable is generated and compile time and unavailable to your code. 唯一的真正区别是,您将必须使用该属性来在类中获取值,因为将生成后备变量,编译时以及代码不可用的情况。

Are you looking for a code refactoring tool? 您在寻找代码重构工具吗? If so, check out ReSharper . 如果是这样, 请查看ReSharper It provides an easy to to turn simple field-backed properties into auto-properties, and vice versa. 它提供了一种轻松将简单的基于字段的属性转换为自动属性的方法,反之亦然。

If you simply don't want to write custom field-backed properties, you can use auto-properties , fpor example, like so: 如果您只是不想编写自定义的字段支持属性,则可以使用auto-properties fpor示例,如下所示:

public string MyProperty { get; set; } // generates an auto-property

which is equivalent to: 等效于:

private string m_MyProperty;
public string MyProperty 
{ 
  get { return m_MyProperty; }
  set { m_MyProperty = value; }
}

You can even make the accessibilty of the setter and getter difference: 您甚至可以使设置程序和获取程序的可访问性有所不同:

public string MyProperty { get; private set; }

If you do choose to use auto-properties, be aware that you cannot access the underlying field, nor can you supply an implementation for just one portion (just the getter or just the setter). 如果确实选择使用自动属性,请注意,您不能访问基础字段,也不能仅提供一部分(仅是getter或setter)的实现。 You can, however, make the property virtual. 但是,您可以将属性设置为虚拟。

仅供参考,只需输入“ prop”(不带引号)就会触发VS附带的代码片段之一,而您只需选择最快捷的方法即可。

Why aren't you doing: 你为什么不这样做:

public int SomeProperty { get; set; }

or 要么

public int SomeOtherProperty { get; private set; }

?

from this line: 从这一行:

string mytest;

select the whole line "string mytest;", then VS menu: Edit > Refactor > Encapsulate field ... you get this: 选择整行“字符串mytest;”,然后选择VS菜单:“编辑”>“重构”>“封装字段”……您将获得以下信息:

public string Mytest { get => mytest; set => mytest = value; }

You probably should be using Auto-Implemented properties in C# for most things. 大多数情况下,您可能应该在C#中使用自动实现的属性 However, if you want 'old-style' properties with explicit backing fields you can create a Visual Studio code snippet to make them easier to write. 但是,如果您想要带有显式后备字段的“旧式”属性,则可以创建一个Visual Studio代码段以使其更易于编写。 This blog post has an example of one. 这篇博客文章有一个例子。

we can quickly create c# properties in visual studio using prop shortcut and behalf of visual studio tool we can generate c# properties using a tool called c# property generator.. 我们可以使用prop快捷方式在Visual Studio中快速创建c#属性,并代表Visual Studio工具,我们可以使用称为c#属性生成器的工具生成c#属性

when class has so many properties in it , when we create a object of that class, we have to take certain pain to assign properties so this tool will reduce your pain to certain extent this will automatically assign object with properties.. 当类中包含许多属性时,当我们创建该类的对象时,必须付出一定的努力才能分配属性,因此此工具将在一定程度上减轻您的痛苦,这将自动为对象分配属性。

c# property assigner c#属性分配器

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

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