Programing

Visual Studio에서 게터 및 세터를 생성하는 방법은 무엇입니까?

lottogame 2020. 4. 28. 08:18
반응형

Visual Studio에서 게터 및 세터를 생성하는 방법은 무엇입니까?


"생성"이란 특정 선택된 변수에 필요한 코드의 자동 생성을 의미합니다.

그러나 모범 사례에 대한 더 명확한 설명이나 의견은 환영합니다.


오히려 사용하는 것보다 ctrl+를 k, x당신은 또한 바로 입력 할 수 있습니다 prop다음 두 번 탭을 명중


Visual Studio에는 개인 변수에서 속성을 생성하는 기능도 있습니다.

변수를 마우스 오른쪽 버튼으로 클릭하면 나타나는 상황에 맞는 메뉴에서 "리 팩터"항목을 클릭하십시오. 그런 다음 캡슐화 필드를 선택하십시오. 변수에 대한 getter / setter 속성이 생성됩니다.

게터 / 세터를 많이 만들어야하는 경우 사용하기가 약간 어색하기 때문에이 기술의 팬은 그리 크지 않으며 개인 필드 바로 아래에 속성을 배치합니다. 내 개인 필드가 모두 그룹화되어 있으며이 Visual Studio 기능을 사용하면 수업 형식이 깨집니다.


Visual Studio 2013 Professional을 사용합니다.

  • 인스턴스 변수 라인에 커서를 놓습니다.

    여기에 이미지 설명을 입력하십시오

  • 결합 키 Ctrl+ R, Ctrl+ E또는 오른쪽 마우스 버튼을 클릭하고 상황에 맞는 메뉴 선택 Refactor \ Encapsulate Field...을 누른 다음을 누릅니다 OK.

    여기에 이미지 설명을 입력하십시오

  • 에서 Preview Reference Changes - Encapsulate Fielddiaglog, 버튼을 누르면 Apply.

    여기에 이미지 설명을 입력하십시오

  • 결과는 다음과 같습니다.

    여기에 이미지 설명을 입력하십시오



당신은 또한 속성을 선택하기 위해 커서를 놓고 메뉴 편집 \ 리 팩터 \ 캡슐화 필드를 사용하십시오 ...

and

private int productID;

public int ProductID
{
    get { return productID; }
    set { productID = value; }
}

become to

public int ProductID { get; set; }

By generate, do you mean auto-generate? If that's not what you mean:

Visual Studio 2008 has the easiest implementation for this:

public PropertyType PropertyName { get; set; }

In the background this creates an implied instance variable to which your property is stored and retrieved.

However if you want to put in more logic in your Properties, you will have to have an instance variable for it:

private PropertyType _property;

public PropertyType PropertyName
{
    get
    {
        //logic here 
        return _property;
    }
    set
    {
        //logic here
        _property = value;
    }
 }

Previous versions of Visual Studio always used this longhand method as well.


you can also use "propfull" and hit TAB twice, variable and property with get and set will be generate.


If you are using Visual Studio 2005 and up you can create a setter/getter real fast using the insert snippet command. Right click on your code click on Insert Snippet (Ctrl+k,x) and then choose "prop" form the list. Hope this helps.


If you're using ReSharper, go into the ReSharper menu --> Code --> Generate ... (or hit Alt+Ins inside the surrounding class) and you'll get all the options for generating getters and/or setters you can think of :-)


use the propfull keyword.
It will generate property and variable


I know this is older than the sun, but figured I would post this as my answer because it just like doing it this way.

What I did was create my own snippet that ONLY adds {get; set;}. I made it just because I find prop > tab to be clunky.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
        <Title>get set</Title>
         <Shortcut>get</Shortcut>
    </Header>
    <Snippet>
        <Code Language="CSharp">
            <![CDATA[{get; set;}]]>
        </Code>
    </Snippet>
  </CodeSnippet>

With this, you type your PropType and PropName manually, then type get > tab and it will add the get set. Its nothing magical, but since I tend to type my access modifier first anyway, I may as well finish out the name and type.


In Visual Studio Community Edition 2015 you can select all the fields you want and then press ctrl + .to automatically generate the properties. You have to choose if you want to use the property instead the field or not.


In addition to the 'prop' snippet and auto-properties, there is a refactor option to let you select an existing field and expose it via a property. Also, if you don't like the 'prop' implementation, you can create your own snippets. Additionally, a 3rd party refactoring tool like resharper will give you even more features and make it easier to create more advanced snippets. I'd recommend Resharper if you can afford it.

http://msdn.microsoft.com/en-us/library/f7d3wz0k(VS.80).aspx http://www.jetbrains.com/


I don't have Visual Studio installed on my machine anymore (and I'm using Linux), but I do remember that there was an wizard hidden somewhere inside one of the menus that gave access to a class builder.

With this wizard, you could define all your classes' details, including methods and attributes. If I remember well, there was an option through which you could ask VS to create the setters and getters automatically for you.

I know it's quite vague, but check it out and you might find it.


여기에 이미지 설명을 입력하십시오behalf of the visual studio tool we can easily generate c# properties using online tool called. c# propery generator.


First get Extension just press (ctrl+shift+X) and install getter setter .... After this just select your variable and right click go to Command palette... And type getter ... It will suggest generate get and set methods click on this...


You just simple press Alt+Ins in android studio after declaring variables, you will get the getters and setters in generating code.

참고 URL : https://stackoverflow.com/questions/3017/how-to-generate-getters-and-setters-in-visual-studio

반응형