Answered by:
C# .NET class getter/setter shorthand

Question
-
User653228039 posted
I remember a friend of mine showing me a nifty shorthand in .NET where you could identify a class's members and getters/setters in a very concise way. Like "get:" and "set:" were in the same code block. Anyone know what I'm talking about? Could someone show me a sample class that has one member and a getter/setter using this shorthand?
Thursday, December 6, 2007 5:23 PM
Answers
-
User1564875471 posted
public class Person { //default constructor public Person() { } private string _Name; public string Name { //set the person name set { this._Name = value; } //get the person name get { return this._Name; } } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 6, 2007 5:28 PM -
User810302116 posted
Hi,
try this
public class MyClass
{
private int _id;
public int Id
{
get { return _id;}
set { _id = value;}
}
}
Regards
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 6, 2007 5:30 PM
All replies
-
User1564875471 posted
public class Person { //default constructor public Person() { } private string _Name; public string Name { //set the person name set { this._Name = value; } //get the person name get { return this._Name; } } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 6, 2007 5:28 PM -
User810302116 posted
Hi,
try this
public class MyClass
{
private int _id;
public int Id
{
get { return _id;}
set { _id = value;}
}
}
Regards
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 6, 2007 5:30 PM -
User653228039 posted
That was the one. Thanks to both of you!
Turns out newer versions of C# supposedly support an even more concise declaration (though I haven't been able to verify because I am on an older server):
public class Person { public string Firstname { get; set; } public string Middlename { get; set; } public string Lastname { get; set; } public int Age { get; set; } }
Now that's concise!
Thursday, December 6, 2007 5:54 PM -
User541272378 posted
I realize I'm way late on this one, but you are correct, C# v3.5 has those shorthand declarations...
public string MyString { get;set; }
...which creates a private variable with the default public accessors (getters/setters), which is basically equivalent to...
private string _myString;
...and unfortunately VB.NET (my client's requirement, argh) has no such shortcut, which is how I came upon this post... trying to find one (pain in the...)
public string MyString
{
get { return _myString; }
set { _myString = value; }
}Friday, May 8, 2009 11:30 PM -
User653228039 posted
Yeah, that is probably the biggest reason I prefer C# over VB. VB code tends to be pretty wordy and it just feels like I'm writing a novel sometimes instead of code. :-P
Monday, May 11, 2009 12:02 PM -
User-1266111725 posted
this is a quick and easy way to do the getter and setter of the field but I am just wondering what is the difference between this approach and the traditional approach in a simple class not in the polymorphism
private int _id; public void SetId (int id) { _id = id; } public int GetId() { return _id; }
Friday, May 28, 2010 12:29 PM -
User-579121670 posted
public string Name { get; set; } is what I use, when i simply need a getter and setter, no need to even declare the private string _name.
Though if you want to do more with your code (check validation, equations, or anything) you need to write it all out
private string _name public void Name(string _name) { _name + " smith" = Name; }
Friday, May 28, 2010 12:46 PM