Where do I put the [XmlAttribute] attribute in c#?
-
25-07-2021 - |
Question
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;}
}
Solution
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;}
OTHER TIPS
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow