Questions tagged [automatic-properties]

219 questions
10
votes
4 answers

What is @synthesize foo = _foo all about?

Why does one want this underscore prefix in an iOS app?
Surfer
  • 109
  • 2
10
votes
3 answers

What's the best way to call INotifyPropertyChanged's PropertyChanged event?

When you implement the INotifyPropertyChanged interface, you're responsible for calling the PropertyChanged event each and everytime a property is updated in the class. This typically leads to the following code : public class MyClass:…
Brann
  • 31,689
  • 32
  • 113
  • 162
10
votes
10 answers

C# Auto Property - Is this 'pattern' best practice?

I seem to be using this sort of pattern in my code a lot , I know that it is not a simple Autoproperty any more as that would be: public IList BCSFilters { get; set; } The code I have been using is this: private IList
Phill Duffy
  • 2,805
  • 3
  • 33
  • 45
10
votes
6 answers

Is there a way to make readonly (not just private) automatic properties?

Automatic properties let me replace this code: private MyType myProperty; public MyType MyProperty { get { return myPropertyField; } } with this code: public MyType MyProperty { get; private set; } with a few changes here and there - but is…
Simon
  • 25,468
  • 44
  • 152
  • 266
10
votes
10 answers

C# Automatic Properties - Still null after +=?

This seems like a bug to me... I accept that automatic properties, defined as such: public decimal? Total { get; set; } Will be null when they are first accessed. They haven't been initialized, so of course they are null. But, even after setting…
Sam Schutte
  • 6,666
  • 6
  • 44
  • 54
9
votes
6 answers

C# , Difference between property with variable and without variable

Possible Duplicate: What's the difference between encapsulating a private member as a property and defining a property without a private member? I know the basic functionality of properties . But as i go through documentation in depth i see they…
Kuntady Nithesh
  • 11,371
  • 20
  • 63
  • 86
9
votes
4 answers

Automatic variables in C++

Possible Duplicate: In C++, why should new be used as little as possible? Where are automatic variables allocated in C++? On the stack or the heap? Also, I read in 7.9 — The stack and the heap that all memory allocated on the stack is known at…
Nitin Garg
  • 2,069
  • 6
  • 25
  • 50
9
votes
8 answers

How to initialize auto-property to not null in C#?

I have a property: public Dictionary MyProp { get; set; } When I invoke that property to add an item, I get a NullReferenceException. How would I do the null check in the property itself so it gives me a new one if it is null? While…
user259286
  • 977
  • 3
  • 18
  • 38
9
votes
3 answers

Auto-properties and structs

I am wondering about the following C#-code: struct Structure { public Structure(int a, int b) { PropertyA = a; PropertyB = b; } public int PropertyA { get; set; } public int PropertyB { get; set; } } It is not…
Peter17
  • 3,052
  • 9
  • 47
  • 77
9
votes
9 answers

Validating properties in c#

let's suggest I got an interface and inherit class from it, internal interface IPersonInfo { String FirstName { get; set; } String LastName { get; set; } } internal interface IRecruitmentInfo { DateTime RecruitmentDate { get; set;…
lexeme
  • 2,915
  • 10
  • 60
  • 125
9
votes
4 answers

Adding a setter to a virtual property in C#

I have a situation like this: public abstract class BaseClass { public abstract string MyProp { get; } } Now, for some of the derived classes, the properties value is a synthesized values, so there is no setter: public class Derived1 :…
James Curran
  • 101,701
  • 37
  • 181
  • 258
9
votes
1 answer

Auto-property initializer Singleton implementation

So, with the brand new C# 6 we got those neat auto-property initializers. I thought I might as well take advantage of these to make more concise singletons than ever. Apparently someone else got that idea, too. public sealed class Singleton { …
Alice
  • 585
  • 5
  • 16
9
votes
2 answers

In C# can I make auto-property perform some extra work with a help of an attribute?

This question is related but not the same as this: How do you give a C# Auto-Property a default value? I love auto-properties, but sometimes I have to do something like this: private string someName; public string SomeName { get { …
Hamish Grubijan
  • 10,562
  • 23
  • 99
  • 147
9
votes
6 answers

Is implementing a singleton using an auto-property a good idea?

I recently found out about auto-properties and like them quite a lot. At this moment I am trying to use them everywhere where I can. Not really to just be able to use them everywhere, but more to see how well they work in most situations. Now I am…
Matthijs Wessels
  • 6,530
  • 8
  • 60
  • 103
9
votes
1 answer

iOS -- "add" methods appearing for autosynthesized properties in codeSense

I just created an iOS class with the following properties: @property (nonatomic, strong) NSString* foo; @property (nonatomic, strong) NSObject* bar; @property (nonatomic) CGRect fubar; I did not put in any @synthesize or explicit ivars for these…
William Jockusch
  • 26,513
  • 49
  • 182
  • 323