backing-field

Why doesn't Type.GetFields() return backing fields in a base class?

孤街醉人 提交于 2020-01-02 03:00:49
问题 In C#, if you use Type.GetFields() with a type representing a derived class, it will return a) all explicitly declared fields in the derived class, b) all backing fields of automatic properties in the derived class and c) all explicitly declared fields in the base class. Why are the d) backing fields of automatic properties in the base class missing? Example: public class Base { public int Foo { get; set; } } public class Derived : Base { public int Bar { get; set; } } class Program { static

How can I set the value of auto property backing fields in a struct constructor?

ε祈祈猫儿з 提交于 2019-12-29 06:38:11
问题 Given a struct like this: public struct SomeStruct { public SomeStruct(String stringProperty, Int32 intProperty) { this.StringProperty = stringProperty; this.IntProperty = intProperty; } public String StringProperty { get; set; } public Int32 IntProperty { get; set; } } Of course, a compiler error is generated that reads The 'this' object cannot be used before all of its fields are assigned to . Is there a way to assign values to the backing fields or the properties themselves, or do I have

Why doesn't Type.GetFields() return backing fields in a base class?

做~自己de王妃 提交于 2019-12-05 06:32:19
In C#, if you use Type.GetFields() with a type representing a derived class, it will return a) all explicitly declared fields in the derived class, b) all backing fields of automatic properties in the derived class and c) all explicitly declared fields in the base class. Why are the d) backing fields of automatic properties in the base class missing? Example: public class Base { public int Foo { get; set; } } public class Derived : Base { public int Bar { get; set; } } class Program { static void Main(string[] args) { FieldInfo[] fieldInfos = typeof(Derived).GetFields( BindingFlags.Public |

Binary Formatter and properties with\\without backing fields

落爺英雄遲暮 提交于 2019-11-29 12:11:12
I have the following class serialized into a file using BinaryFormatter: [Serializable] public class TestClass { public String ItemTwo { get; set; } public String ItemOne { get; set; } } Using this code: FileStream fs = new FileStream("DataFile.dat", FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, new TestClass{ItemOne = "ItemOne", ItemTwo = "ItemTwo"}); fs.Close(); When deserializing using this code: FileStream fs = new FileStream("DataFile.dat", FileMode.Open); BinaryFormatter formatter = new BinaryFormatter(); TestClass addresses = (TestClass

How can I set the value of auto property backing fields in a struct constructor?

放肆的年华 提交于 2019-11-29 05:33:44
Given a struct like this: public struct SomeStruct { public SomeStruct(String stringProperty, Int32 intProperty) { this.StringProperty = stringProperty; this.IntProperty = intProperty; } public String StringProperty { get; set; } public Int32 IntProperty { get; set; } } Of course, a compiler error is generated that reads The 'this' object cannot be used before all of its fields are assigned to . Is there a way to assign values to the backing fields or the properties themselves, or do I have to implement properties the old-fashioned way with my own explicit backing fields? Prior to C# 6, you

Binary Formatter and properties with\without backing fields

纵然是瞬间 提交于 2019-11-28 05:29:41
问题 I have the following class serialized into a file using BinaryFormatter: [Serializable] public class TestClass { public String ItemTwo { get; set; } public String ItemOne { get; set; } } Using this code: FileStream fs = new FileStream("DataFile.dat", FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, new TestClass{ItemOne = "ItemOne", ItemTwo = "ItemTwo"}); fs.Close(); When deserializing using this code: FileStream fs = new FileStream("DataFile.dat",

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.