문제

I am changing some public field to private by providing corresponding public property.

for example:

public string Name;

is change to

private string _name;
public string Name
{
    get{ return _name;}
    set{ _name = value;}
}

However, what about the [XmlAttribute] attribute on the original public field? i.e. does

[XmlAttribute]
public string Name;

becomes:

[XmlAttribute]
private string _name;
public string Name
{
    get{ return _name;}
    set{ _name = value;}
}

or

private string _name;
[XmlAttribute]
public string Name
{
    get{ return _name;}
    set{ _name = value;}
}
도움이 되었습니까?

해결책

second solution.

By the way, you don't need anymore (.net 3.0 and +) a private field, if you don't do anything special in getter or setter.

You can use auto-implemented properties.

[XmlAttribute]
public string Name {get;set;}

다른 팁

And instead of doing

private string _name;
public string Name
{
get{ return _name;}
set{ _name = value;}
}

why not do

public string Name { get; set; }

I think this was introduced in C# 3.0

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top