automatic-properties

C#3.0 Automatic properties with extra logic

血红的双手。 提交于 2019-11-29 11:40:06
How can I rewrite he following code using C#3.0 automatic properties? private int _myValue; public int MyProperty { get { return _myValue;} set { if (value > 0) { _myValue = value; } } } If it is not possible, What is the alternative? No, automatically implemented properties have no declared implementation. Any extended implementation that you wish to provide would have to use a regular property. I am not sure what you are looking for in terms of an alternative - the syntax you have used in your question is the alternative. Answer: You can't do this with automatic properties. 来源: https:/

Explicit implementation of an interface using an automatic property

让人想犯罪 __ 提交于 2019-11-29 09:19:51
Is there any way to implement an interface explicitly using an automatic property? For instance, consider this code: namespace AutoProperties { interface IMyInterface { bool MyBoolOnlyGet { get; } } class MyClass : IMyInterface { static void Main(){} public bool MyBoolOnlyGet { get; private set; } // line 1 //bool IMyInterface.MyBoolOnlyGet { get; private set; } // line 2 } } This code compiles. However, if you replace line 1 with line 2 it does not compile. (It's not that I need to get line 2 working - I'm just curious.) Indeed, that particular arrangement (explicit implementation of a get

C# Automatic Properties - Why Do I Have To Write “get; set;”?

北城余情 提交于 2019-11-29 02:06:29
问题 If both get and set are compulsory in C# automatic properties, why do I have to bother specifying "get; set;" at all? 回答1: ERROR: A property or indexer may not be passed as an out or ref parameter If you didn't specify {get; set;} then the compiler wouldn't know if it's a field or a property. This is important becasue while they "look" identical the compiler treats them differently. e.g. Calling "InitAnInt" on the property raises an error. class Test { public int n; public int i { get; set; }

xCode 6 how to fix “Use of undeclared identifier” for automatic property synthesis?

夙愿已清 提交于 2019-11-28 22:23:54
I'm using xCode6 Beta 3, and am running into an issue where a code which previously compiled fine (xCode 5.1.1 or xCode6 beta 2) suddenly started to give me "Use of undeclared identifier" errors when accessing an automatically synthesized instance variable: - (void)setFinished:(BOOL)finished { [self willChangeValueForKey:@"isFinished"]; _finished = finished; [self didChangeValueForKey:@"isFinished"]; } //ERROR: Use of undeclared identifier '_finished'; did you mean 'finished'? Adding @synthesize finished = _finished; makes the error go away, but is there a way to force xCode6 Beta 3 to use

How to prevent auto implemented properties from being serialized?

試著忘記壹切 提交于 2019-11-28 10:40:09
How can I prevent a auto implemented property from being serialized by the binary formatter? The [NonSerialized] attribute can only be used with fields. And the field is hidden when using auto implemented properties. It´s not supported for auto implemented properties. You have to use a backing field and set the NonSerializedAttribute on it. public class ClassWithNonSerializedProperty { [NonSerialized] private object _data; // Backing field of Property Data that is not serialized public object Data{ get { return _data; } set { _data = value; } } } // This works for the underlying delegate of

C#3.0 Automatic properties with extra logic

馋奶兔 提交于 2019-11-28 05:33:48
问题 How can I rewrite he following code using C#3.0 automatic properties? private int _myValue; public int MyProperty { get { return _myValue;} set { if (value > 0) { _myValue = value; } } } If it is not possible, What is the alternative? 回答1: No, automatically implemented properties have no declared implementation. Any extended implementation that you wish to provide would have to use a regular property. I am not sure what you are looking for in terms of an alternative - the syntax you have used

C# Lazy Loaded Automatic Properties

[亡魂溺海] 提交于 2019-11-28 03:37:54
In C#, Is there a way to turn an automatic property into a lazy loaded automatic property with a specified default value? Essentially, I am trying to turn this... private string _SomeVariable public string SomeVariable { get { if(_SomeVariable == null) { _SomeVariable = SomeClass.IOnlyWantToCallYouOnce(); } return _SomeVariable; } } into something different, where I can specify the default and it handles the rest automatically... [SetUsing(SomeClass.IOnlyWantToCallYouOnce())] public string SomeVariable {get; private set;} JaredPar No there is not. Auto-implemented properties only function to

Explicit implementation of an interface using an automatic property

﹥>﹥吖頭↗ 提交于 2019-11-28 02:43:30
问题 Is there any way to implement an interface explicitly using an automatic property? For instance, consider this code: namespace AutoProperties { interface IMyInterface { bool MyBoolOnlyGet { get; } } class MyClass : IMyInterface { static void Main(){} public bool MyBoolOnlyGet { get; private set; } // line 1 //bool IMyInterface.MyBoolOnlyGet { get; private set; } // line 2 } } This code compiles. However, if you replace line 1 with line 2 it does not compile. (It's not that I need to get line

How to set value of property where there is no setter

做~自己de王妃 提交于 2019-11-28 01:59:20
I have seen various questions raised and answered where we can invoke a private setter using reflection such as this one: Is it possible to get a property's private setter through reflection? However I have some code which has a property i need to set but cant because there is no setter, I cant add a setter as this isn't my code. Is there a way to somehow set the value using reflection in this scenario? I do not suggest doing this on your application but for testing purpose it may be usefull... Assuming you have: public class MyClass { public int MyNumber {get;} } You could do this if its for

Are C# auto-implemented static properties thread-safe?

廉价感情. 提交于 2019-11-27 22:45:59
I would like to know if C# automatically implemented properties, like public static T Prop { get; set; } , are thread-safe or not. Thanks! It appears not. This is the decompilation with Reflector: private static string Test { [CompilerGenerated] get { return <Test>k__BackingField; } [CompilerGenerated] set { <Test>k__BackingField = value; } } Section 10.7.4 of the C# specification states: When a property is specified as an automatically implemented property, a hidden backing field is automatically available for the property, and the accessors are implemented to read from and write to that