Questions tagged [automatic-properties]
219 questions
6
votes
6 answers
C# Automatic Properties -- setting defaults
What's the easiest/straight-forward way of setting a default value for a C# public property?
// how do I set a default for this?
public string MyProperty { get; set; }
Please don't suggest that I use a private property & implement the get/set…

Alan
- 2,574
- 2
- 18
- 16
6
votes
6 answers
C# 3.0 Auto-Properties - Is it possible to add custom behaviour?
I would like to know if there is any way to add custom behaviour to the auto property get/set methods.
An obvious case I can think of is wanting every set property method to call on any PropertyChanged event handlers as part of a…

morechilli
- 9,827
- 7
- 33
- 54
5
votes
9 answers
Do you think "auto interface implementation" would be useful in .NET / C#
Consider this:
public class interface Person : IPerson
{
int ID { get; protected set; }
string FirstName { get; set; }
string LastName { get; set; }
string FullName { get { return FirstName + " " + LastName; } }
}
And this:
public class…

Jonathan Parker
- 6,705
- 3
- 43
- 54
5
votes
1 answer
MonoTouch - Fields vs Automatic Properties
Is there a noticeable performance difference when using fields instead of auto properties? What about if I'm deserializing an array of say, 1000 JSON objects with 5 properties each?
My iPhone domain model is basically a copy of the DTOs my web…

kwcto
- 3,494
- 2
- 26
- 33
5
votes
5 answers
C# autoproperty vs normal fields
is there any effective difference between Foo.Something and Bar.Something in this example?
class Foo
{
public string Something;
}
class Bar
{
public string Something{get; set;}
}
class Program
{
static void Main(string[] args)
{
…

adekcz
- 340
- 2
- 13
5
votes
2 answers
Change auto implemented properties to normal and deserialization with BinaryFormatter
I have an object with a property implemented like
public String Bla {get;set;}
After changing the implementation to something like
private String _bla;
public String Bla
{
get { return _bla; }
set { _bla = value; }
}
on…

Dr. Wummi
- 111
- 7
4
votes
1 answer
Auto updating properties in sqlalchemy
I've got a sqlalchemy model that is set up like this:
class Entry(Base):
__tablename__ = 'entries'
__table__ = Table('entries', Base.metadata,
Column('id', Integer, primary_key=True, unique=True),
Column('user_id',…

X-Istence
- 16,324
- 6
- 57
- 74
4
votes
4 answers
Is there a technical reason why an automatic property must define both a get and set accessor
I know that automatic properties must define a get and set accessor method, I also know that it is possible for either of these accessors to be made invisible by means of an access modifier.
Is there a technical reason why the compiler is happy…

Crippledsmurf
- 3,982
- 1
- 31
- 50
4
votes
1 answer
.NET - Error trying to compile Automatic Properties
I'm trying to compile a POCO with this code
public class MenuItem
{
public string Name
{ get; set; }
public string Url
{ get; set; }
}
I keep getting compile errors on the gets and sets with messages like:
'MenuItem.Name.get'…

BuddyJoe
- 69,735
- 114
- 291
- 466
4
votes
5 answers
Why do automatic properties require both getters AND setters?
In C#, if I declare an auto-implemented property, why do I have to declare BOTH the get and set part?
i.e.
public string ThisWorks { get; set; }
public string ThisDoesnt { get; }
Isn't this just syntactic sugar - i.e. the compiler inserts a…

Duncan
- 10,218
- 14
- 64
- 96
4
votes
4 answers
Automatic transformation from getter/setter to properties
I have a big library written in C++ and someone created an interface to use it in python (2.6) in an automatic way. Now I have a lot of classes with getter and setter methods. Really: I hate them.
I want to re-implement the classes with a more…

Ruggero Turra
- 16,929
- 16
- 85
- 141
4
votes
2 answers
Why static automatic properties is only useful in which the getter is public and setter is private
In book <>, I have read a sentence "The only scenario in which I can see static automatic properties being useful is where the getter is public and setter is private, and the setter is only called whithin the type initializer". I am not…

ValidfroM
- 2,626
- 3
- 30
- 41
4
votes
6 answers
Learning about Auto-Implemented Properties
I have the simple class using auto-implemented properies:
Public Class foo
{
public foo() { }
public string BarName {get; set;}
}
I obviously use the variable BarName throughout my class and now need to add logic when the property…

Brettski
- 19,351
- 15
- 74
- 97
4
votes
2 answers
Does JavaScript have an equivalent way to implement c# style auto properties without massive overhead?
I'm curious if JavaScript has a way to bind a function when a property is changed without an enormous overhead like watching all the auto-properties with a timer, but without setting them via a function call. For example, I know you could do…

CoryG
- 2,429
- 3
- 25
- 60
4
votes
2 answers
Are automatic properties for private variables unnecessary?
I was look at somebody else's code and came across this piece of code
private string _deviceName { get; set; }
private string _deviceAlias { get; set; }
My thinking is that those automatic properties for private variables are unnecessary. Am I…

devcoder
- 1,675
- 2
- 21
- 28