I have a problem in the way how Visual Studio refactors uppercase properties.
Here is some simple class:
public class TEST
{
public string MY_STRING { get; set; }
}
Now i using CTRL+ to refactor my property -> "Convert to full property"
Visual Studio is creating code
public class TEST
{
private string mY_STRING;
public string MY_STRING { get => mY_STRING; set => mY_STRING = value; }
}
I would like my fields to be lowercase, like this:
public class TEST
{
private string my_string;
public string MY_STRING { get => my_string; set => my_string = value; }
}
Is there any way to change this behavior?
First, you need to choose field text and Ctrl+U.
Second, you use Ctrl+R, E to refactor properties.
Result:
Related
I prefer this:
private string _indeks;
public string Indeks
{
get => _indeks;
set => _indeks = value;
}
Instead of:
public string Indeks { get; set; }
But I haven't found a way to enforce that code style in Resharper in "Code Cleanup"?
Does anyone know how to do that?
After compiling and running the program with:
class Person
{
private string surname;
public string Name { get; set; }
public string Surname
{
get { return surname;}
set
{
surname = value;
}
}
}
One can see, from the image linked, there is no "name" private field shown.
Is it just Visual Studio not recognizing it, or is there something else going on?
I have read numerous times things like "As for your two C# examples, one is simply syntactic sugar for the other." or "A backing field will be created when compiling.".
What's the catch?
The backing field isn't shown to you by the debugger. That doesn't mean it isn't there, it simply means that the designers of the debugger didn't feel it was important for people debugging code to be looking at private backing fields of auto properties, given that they can simply access the data through the property itself.
With a help of reflection you can get a report what's actually going on:
using System.Reflection;
...
var fields = typeof(Person)
.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Select(field => field.Name);
Console.Write(string.Join(Environment.NewLine, fields));
And get
surname
<Name>k__BackingField
As you can see the compiler has created the backing field <Name>k__BackingField for the auto property
I compiled a project with this silly class:
using System;
namespace ApagueMe
{
public class Class1
{
public string Asdf { get; set; }
}
}
Then, i opened the generated DLL in .NET Refletor. Look at the result:
public class Class1
{
// Fields
[CompilerGenerated, DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string <Asdf>k__BackingField;
// Properties
public string Asdf
{
[CompilerGenerated]
get
{
return this.<Asdf>k__BackingField;
}
[CompilerGenerated]
set
{
this.<Asdf>k__BackingField = value;
}
}
}
What i am trying to do, is to translate an application that uses attributes to set text in controls. I was thinking about custom reources manager but attributes has to be hardcoded.
My question is:
Is there any way to change visible text set by an attribute using PostSharp and where are the attributes stored in runtime?
e.g. for code
[DataMember]
[DisplayName("Mission description")]
[Description("Description of this mission")]
public string Description { get; set; }
What do i want to achive is to extract "Mission description" and "Description of this mission" to external file, translate it, and pass new translated values to Description String as an Attribute during execution of program.
What i had to do was to create a class that inherits from System.ComponentModel.DisplayNameAttribute, name it "DisplayNameAttribute" to override parent class, and overwrite parent class constructor, "DisplayName" and "DisplayNameValue" properties.
Next I put my logic into DisplayNameValue getter.
Then create DescriptionAttribute class by analogy.
public class DisplayNameAttribute : System.ComponentModel.DisplayNameAttributes
{
private string name;
public DisplayNameAttribute() { }
public DisplayNameAttribute(String name) { this.name = name; }
public override string DisplayName
{
get
{
return DisplayNameValue;
}
}
public string DisplayNameValue
{
get
{
/* e.g logic for reading from dictionary file */
return myDictionary[name];
}
set
{
name = value;
}
}
}
}
Where "string name" is where i hold my key to Dictionary.
This question already has answers here:
Public Fields versus Automatic Properties
(14 answers)
Closed 10 years ago.
I am creating a simple User class, does it matter if I use public properties with private fields verses just using public fields?
Here is an example of what I mean:
public class clsUser
{
private string name;
private string lName;
public string Name
{
get
{
return name;
}
set
{
name= value;
}
}
public string LName
{
get
{
return lName;
}
set
{
lName= value;
}
}
public clsUser(string userID)
{
//get the user id here and set the properties
this.name= getName(userID);
this.lName= getLName(userID);
}
}
or can I just make
public string name;
public string lName;
public and now worry about typing out all of these:
public string Name
{
get
{
return name;
}
set
{
name= value;
}
}
I am then going to populate a form using these on another page like so:
clsUser cUser - new clsUser("myid");
txtSomething.Text = cUser.name;
and so on...
I guess my question is why do I need to retype the properties first as private and then as public (as I've seen in all web examples). Why not just make them public to begin with?
You're confusing fields with properties.
String name; is a field.
Unlike a property, you have no control over it.
If you eventually decide to add validation or change events or other logic, you'll need to change it to a property, which will break compiled code.
Certain features (eg, bindings) also can only work with properties.
Instead, you can use auto-implemented properties to make the compiler generate all of that boilerplate:
public String Name { get; set; }
This must have been asked many times but I cannot find it....sorry...
Why is the following not permitted?
public string MyString = "initial value" {get; private set;}
(Visual C# Express 2010)
It's just not valid syntax. You can't initialize the value of an auto-property, unfortunately.
The best options are to either make the property manually:
private string _MyString = "initial value";
public string MyString { get { return _MyString; } set { _MyString = value; } }
or initialize the value in the constructor:
public string MyString { get; set; }
....
public MyClass() {
MyString = "initial value";
}
An alternative:
string _strMyString;
public string MyString
{
get {
if (String.IsNullOrEmpty(_strMyString) {
return "initial value";
} else {
return _strMyString;
}
}
It's a property, not a field. You can't initialize it this way. Just set the value in the constructor.
the syntax
public string MyString { get; set; }
is replacing the old style / annoying / trivial (as of vs2008/c# 3.0 you can see all the new features of c# 3.0 here)
private string _MyString;
public string MyString
{
get { return _MyString; }
set { _MyString = value; }
}
the compiler is actually generates a member before compiling your code.
you can open a reflector and see the generated member.
Why?
I cannot speak on behalf of the designers of C#, but I can make educated speculation:
They wanted to see just how big of a deal it is before taking the time to
add another feature and yet another rule to the language.
They could not find a sufficiently elegant-looking way to do this.
That said, here is how I would allow values (when a set accessor is available, of course):
public string MyProp {get;set;} = "initial value"; // not valid C#
Without making the language any more complex, they could write the rule so that it applies to "[all] properties with set accessors" instead of to "default properties with set accessors":
// again, not valid C#:
public string MyProp
{
get { return _MyProp;}
set { _MyProp = value; }
} = "initial value before being massaged or rejected by the set accessor.";
The only downside I see here is that it is ugly. The benefits are that you can concisely specify an initial value for a property with that property instead of in the constructor, and that you can let the value be massaged / checked / whatever via constructor at runtime if you wish.
In VS 2017 you can:
public int Minimum { get; private set; } = 0;
public int Maximum { get; set; } = 5;