automatic-properties

No ivars -> What am I missing?

為{幸葍}努か 提交于 2019-12-04 03:46:33
I never use ivars. I only use properties -- sometimes assign properties with primitive types, and sometimes on a "private" class extension . I've seen the advantages of not using ivars in switching to ARC -- I have some borrowed code with lots of ivars that I still can't "ARC", since I don't know what needs to be retained. So I know some advantages of not using ivars, but what are the advantages of using ivars instead of properties? Note: I depend exclusively on the ivars that are automagically added in (by the compiler?) for the property declaration. Don't mark to close: I've looked at some

iOS — “add” methods appearing for autosynthesized properties in codeSense

妖精的绣舞 提交于 2019-12-04 03:01:08
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 properties. I then went into the implementation file and started to create a method as follows: -(void) add I left the cursor at the end of the word "add". The following method names then popped up in code sense: addBar: (NSSet*) objects addBarObject: (objectType *) object addFoo: (NSSet*) objects addFooObject: (objectType *) object addFubar: (NSSet*)

Getter without body, Setter with

半城伤御伤魂 提交于 2019-12-03 23:35:45
问题 I have a property which is currently automatic. public string MyProperty { get; set; } However, I now need it to perform some action every time it changes, so I want to add logic to the setter. So I want to do something like: public string MyProperty { get; set { PerformSomeAction(); } } However, this doesn't build... MyProperty.get' must declare a body because it is not marked abstract, extern, or partial I can't just have the getter return MyProperty as it will cause an infinite loop. Is

Do you think “auto interface implementation” would be useful in .NET / C# [closed]

ぃ、小莉子 提交于 2019-12-03 20:14:45
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . Consider this: public class interface Person : IPerson { int ID { get; protected set; } string FirstName { get; set; } string LastName

How to set default value for Auto-Implemented Properties in ASP.NET [duplicate]

泪湿孤枕 提交于 2019-12-03 09:34:36
This question already has answers here : What is the best way to give a C# auto-property an initial value? (21 answers) I came to know that C# 3.0 comes with a new feature of Auto-Implemented Properties,I liked it as we don't have to declare extra private varible in this (compare to earlier property), earlier I was using a Property i.e. private bool isPopup = true; public bool IsPopup { get { return isPopup; } set { isPopup = value; } } Now I've converted it into Auto-Implemented property i.e. public bool IsPopup { get; set; } I want to set the default value of this property to true without

Why is it necessary to call :this() on a struct to use automatic properties in c#?

百般思念 提交于 2019-12-03 08:04:29
问题 If I define a struct in C# using automatic properties like this: public struct Address { public Address(string line1, string line2, string city, string state, string zip) { Line1 = line1; Line2 = line2; City = city; State = state; Zip = zip; } public string Line1 { get; protected set; } public string Line2 { get; protected set; } public string City { get; protected set; } public string State { get; protected set; } public string Zip { get; protected set; } } When I attempt to build the file,

Why is it necessary to call :this() on a struct to use automatic properties in c#?

吃可爱长大的小学妹 提交于 2019-12-02 23:15:31
If I define a struct in C# using automatic properties like this: public struct Address { public Address(string line1, string line2, string city, string state, string zip) { Line1 = line1; Line2 = line2; City = city; State = state; Zip = zip; } public string Line1 { get; protected set; } public string Line2 { get; protected set; } public string City { get; protected set; } public string State { get; protected set; } public string Zip { get; protected set; } } When I attempt to build the file, I get a compilation error saying The 'this' object cannot be used before all of its fields are assigned

Encapsulate Collection in C#

此生再无相见时 提交于 2019-12-02 20:21:47
Since 3.0 C# has great syntax sugar like auto-properties which a lot simplify implementation of encapsulation principle. This is good if you use it with atomic values, so you can replace encapsulation pattern like this: private string _name; public string Name { get { return _name; } set { _name = value; } } with just one line: public string FirstName { get; set; } I very like this great feature as it saves a lot of developers time. But things are not so great when you create property that points to collection. Usually I see collection properties implemented in one of two ways. 1) Without auto

Object fingerprinting: serialization + untouchable legacy code + Getter-only auto-properties = cornered?

一曲冷凌霜 提交于 2019-12-02 14:21:12
I have found myself cornered, so here we go. Context I need to produce a fingerprint hash code for object diffing. Comparing the hashes of two sets of objects will need to tell me if there are identical objects with the same hash. The fingerprint hash must be platform-independent . So I went for MD5 hashing . I am working with a large Object model code base that is out of my control. All types that I will be passed for this fingerprinting can not be modified by me . I cannot add attribute or constructors or modify anything. That does not exclude that the types will change in the future. So any

Can I create an automatic property (no private member) with get and set code?

元气小坏坏 提交于 2019-12-02 12:11:35
问题 In c#, I can do this: public int Foo { get; set; } Which is nice. But as soon as I want to do anything in the getter or setter, I have to change it to this: private int foo; public int Foo { get { return foo; } set { foo = value; DoSomething(); // all that other code, just to add this line! } } Is it possible to avoid this? I would love to be able to do something like this: public int Foo { get; set { DoSomething(); } } How close can I get to the above? 回答1: No, there's no way to do this with