Using a private auto property instead of a simple variable for a programming standard

北战南征 提交于 2019-12-12 10:30:20
问题 In a discussion with a peer, it was brought up that we should consider using auto properties for all class level variables... including private ones. So in addition to a public property like so: public int MyProperty1 { get; set; } Our private class-level variables would look like this: private int MyProperty2 { get; set; } Instead of: private int _myProperty2; I'm on the fence about why someone would want to do this but I can't decide if my reluctance to accept this is because of my own

The case against automatic properties [duplicate]

岁酱吖の 提交于 2019-12-11 09:28:43
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: C# 3.0 Auto-Properties - useful or not? My boss and I regularly argue about the benefits and disadvantages of using automatic properties. public string Name { get; set; } vs private string name; public string Name { get { return; } set { = value; } } For I am strongly in favor of using them because I have to write less code , I find it easier to understand the class when all the fields are

auto increment alphanumeric characters php

穿精又带淫゛_ 提交于 2019-12-11 05:57:58
问题 Is is possible to auto increment an alphanumeric number with php so it looks like: AB001 AB002 ... ... BA001 ... ... ZZ001 This code will then need to be added to mysql, I was thinking a varchar(5). Cheers, 回答1: Try it and see $x = 'AA998'; for($i = 0; $i < 10; $i++) { echo $x++,'<br />'; } EDIT Reverse of letters and digits $x = '000AY'; for($i = 0; $i < 10; $i++) { echo $x++,'<br />'; } or reversal after ZZ999 $x = 'ZZ998'; for($i = 0; $i < 10; $i++) { $x++; if (strlen($x) > 5) $x = '000AA'

C# automatic property

安稳与你 提交于 2019-12-10 18:15:25
问题 Does the automatic property of C# 3.0 completely replace the filed? I mean,I can directly use the property instead of filed as property serves as private backing field.(sorry,I understand like that only). int a; public int A { get;set; } 回答1: When you access the property from code - whether inside or outside the class - it is always accessed as a property. In most cases, this is unimportant - but it does mean that you can't pass it by reference, which you would be able to do if it were a

How often do you see abuse of C# shorthand getters/setters?

守給你的承諾、 提交于 2019-12-10 04:06:18
问题 In C# you can create getter/setters in a simpler way than other languages: public int FooBar { get; set; } This creates an internal private variable which you can't address directly, with the external property 'FooBar' to access it directly. My question is - how often do you see this abused? It seems like it has a high potential to violate encapsulation best-practices often. Don't get me wrong, I use it as appropriate, and partial variations of it for read-only write-only types of properties,

No ivars -> What am I missing?

倖福魔咒の 提交于 2019-12-09 16:37:43
问题 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

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

十年热恋 提交于 2019-12-09 07:41:15
问题 This question already has answers here : What is the best way to give a C# auto-property an initial value? (22 answers) Closed 5 years ago . 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

Objective C multiple declarations of instance variables / properties

六眼飞鱼酱① 提交于 2019-12-08 01:14:27
问题 I'm still pretty new to ObjC. I noticed that it's pretty standard everywhere to create your @interface myObj : NSObject { id delegate; NSDictionary *dict; } and then @property (nonatomic,retain) NSDictionary *dict; @property (retain) id delegate; --for example. I know how useful the auto code generation + clearer definition of @property is thanks to the Declared Properties page over at Apple. What I do not understand, however, is why it's standard for people to do both -- declare their

Auto-property initializer Singleton implementation

断了今生、忘了曾经 提交于 2019-12-06 22:46:44
问题 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 { public static Singleton Instance { get; } = new Singleton(); private Singleton() { /* some initialization code */ } } My questions are: How thread-safe it is? How lazy it is, or when the instance is actually created? (not a priority, but it would be good

Code contracts on auto-implemented properties

你离开我真会死。 提交于 2019-12-06 18:29:37
问题 Is there any way to put contracts on automatically implemented properties in .NET? (And how if the answer is 'Yes')? (I assume using .NET code contracts from DevLabs) 回答1: Yes, this is possible - all that is needed is to add your contract condition to the [ContractInvariantMethod] method in your class, which then adds the equivalent Requires precondition to the automatic set ter, and a post condition Ensures is added to the get . From section 2.3.1 of the Reference As the example illustrates,