automatic-properties

Auto-properties with or without backing field - preference?

六月ゝ 毕业季﹏ 提交于 2019-11-27 21:45:02
问题 I know that when using auto-properties, the compiler creates its own backing field behind the screen. However, in many programs I read to learn from, I see people explicitly write private int _backingField; public int Property { get { return _backingField; } } What is the difference between above, and below? public int Property { get; private set; } I understand that its obvious to use the property when you actually have side-effects in the getter or setter, but that's often not the case.

C# Automatic Properties

不打扰是莪最后的温柔 提交于 2019-11-27 19:14:20
I'm a bit confused on the point of Automatic properties in C# e.g public string Forename{ get; set; } I get that you are saving code by not having to declare a private variable, but what's the point of a property when you are not using any get or set logic? Why not just use public string Forename; I'm not sure what the difference between these 2 statements is, I always thought you used properties if you wanted additional get/set logic? MatthewMartin Properties can have code put into them without breaking contract, fields can't have code put into them without changing them to properties (and

Adding Auto-Implemented Property to class using Roslyn

送分小仙女□ 提交于 2019-11-27 16:19:26
问题 I'm trying to learn Roslyn by building an existing but simple application from the ground up, which seems to be a productive way to learn this. Anyhow, I have the following code: var root = (CompilationUnitSyntax)document.GetSyntaxRoot(); // Add the namespace var namespaceAnnotation = new SyntaxAnnotation(); root = root.WithMembers( Syntax.NamespaceDeclaration( Syntax.ParseName("ACO")) .NormalizeWhitespace() .WithAdditionalAnnotations(namespaceAnnotation)); document = document

Automatic Properties and Structures Don't Mix?

好久不见. 提交于 2019-11-27 11:38:41
问题 Kicking around some small structures while answering this post, I came across the following unexpectedly: The following structure, using an int field is perfectly legal: struct MyStruct { public MyStruct ( int size ) { this.Size = size; // <-- Legal assignment. } public int Size; } However, the following structure, using an automatic property does not compile: struct MyStruct { public MyStruct ( int size ) { this.Size = size; // <-- Compile-Time Error! } public int Size{get; set;} } The error

Automatically implemented property in struct can not be assigned

▼魔方 西西 提交于 2019-11-27 08:02:28
I have a next code: struct T { public T(int u) { this.U = 10; //Errors are here } public int U { get; private set; } } C# compiler give me a pair of errors in stated line: 1) Backing field for automatically implemented property 'TestConsoleApp.Program.T.U' must be fully assigned before control is returned to the caller. Consider calling the default constructor from a constructor initializer. 2) The 'this' object cannot be used before all of its fields are assigned to What I do wrong? Help me understand. From the C# Specification: 10.7.3 Automatically implemented properties When a property is

Use of properties vs backing-field inside owner class

让人想犯罪 __ 提交于 2019-11-27 06:00:34
问题 I love auto-implemented properties in C# but lately there's been this elephant standing in my cubicle and I don't know what to do with him. If I use auto-implemented properties (hereafter "aip") then I no longer have a private backing field to use internally. This is fine because the aip has no side-effects. But what if later on I need to add some extra processing in the get or set? Now I need to create a backing-field so I can expand my getters and setters. This is fine for external code

C# Lazy Loaded Automatic Properties

邮差的信 提交于 2019-11-27 05:09:37
问题 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

How to find out if a property is an auto-implemented property with reflection?

瘦欲@ 提交于 2019-11-27 04:29:36
So in my case i am doing discovery of the structure of a class using reflection. I need to be able to find out if a property is an auto-implemented property by the PropertyInfo object. I assume that the reflection API does not expose such functionality because auto-properties are C# dependent, but is there any workaround to get this information? You could check to see if the get or set method is marked with the CompilerGenerated attribute. You could then combine that with looking for a private field that is marked with the CompilerGenerated attribute containing the name of the property and the

How to prevent auto implemented properties from being serialized?

本秂侑毒 提交于 2019-11-27 03:44:00
问题 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. 回答1: 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

DefaultValue attribute is not working with my Auto Property

六月ゝ 毕业季﹏ 提交于 2019-11-27 02:36:07
问题 I have the following Auto Property [DefaultValue(true)] public bool RetrieveAllInfo { get; set; } when I try to use it inside the code i find the default false for is false I assume this is the default value to a bool variable, does anyone have a clue what is wrong!? 回答1: The DefaultValue attribute is only used to tell the Visual Studio Designers (for example when designing a form) what the default value of a property is. It doesn't set the actual default value of the attribute in code. More