Questions tagged [backing-field]

36 questions
21
votes
2 answers

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

Given a struct like this: public struct SomeStruct { public SomeStruct(String stringProperty, Int32 intProperty) { this.StringProperty = stringProperty; this.IntProperty = intProperty; } public String StringProperty…
Daniel Schaffer
  • 56,753
  • 31
  • 116
  • 165
13
votes
3 answers

What's the standard naming convention for a property's backing field?

Background I'm wanting to follow the most commonly-practised naming conventions for TypeScript. I've noticed that the official website shows code examples featuring Pascal-case for types and modules and camel-case for just about everything…
Sam
  • 40,644
  • 36
  • 176
  • 219
11
votes
1 answer

Auto-properties with or without backing field - preference?

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…
Taelia
  • 591
  • 3
  • 20
10
votes
3 answers

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

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…
Cygon
  • 9,444
  • 8
  • 42
  • 50
5
votes
5 answers

Instantiate a variable in Kotlin only if it is a null?

Lets say, I have a variable: var myObject : MyObject? = null it should be cleared in some place : myObject?.clear myObject = null and should be definitely non-nullable in a place of usage. In Java I can do something like this: private MyObject…
Jack Jones
  • 117
  • 1
  • 6
5
votes
1 answer

How to rename a property's backing field in Kotlin

EDIT (again): If anyone is interested, you can follow this issue on the tracker. EDIT: I know about backing properties and that they will cover most use cases. I'm not looking for a work around, I'm specifically looking if there is a way to name…
Ruckus T-Boom
  • 4,566
  • 1
  • 28
  • 42
5
votes
1 answer

Binary Formatter and properties with\without backing fields

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…
atlanteh
  • 5,615
  • 2
  • 33
  • 54
4
votes
4 answers

C# style: May properties be grouped with their backing fields?

I like to organize simple properties like this: private int foo; public int Foo { get { return foo; } set { // validate value foo = value; } } I've been playing around with StyleCop, and it yells at me for placing…
Matthew
  • 28,056
  • 26
  • 104
  • 170
4
votes
2 answers

Reference to the backing field of auto-implemented property

In C#, auto-implemented properties are quite a handy thing. However, although they do nothing but encapsulate their backing field, they still cannot be passed as ref or out arguments. For example: public int[] arr { get; private set; } /* Our…
user3079266
3
votes
1 answer

Setting a property {get;} using reflection in C#

I have a class from a third-party library with a read-only property called Name. Here is the code for the class: public class Person { public string Name {get;} } I want to set the value of the Name property using reflection or another suitable…
Vahid
  • 5,144
  • 13
  • 70
  • 146
3
votes
1 answer

How to hide the backing field from the rest of the class

Is there any way to enforce the rest of my class to access the property setter rather than the backing field? Consider the following clumsy code: public class Brittle { private string _somethingWorthProtecting; public string…
kmote
  • 16,095
  • 11
  • 68
  • 91
3
votes
2 answers

How do you expose a dependency property of a private, internal object via the interface of the object that contains it?

We have a custom panel class that animates its children via an internal DoubleAnimation object. However, we want to expose the animation's Duration dependency property as a public property of our panel so the user can change it in their XAML when…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
3
votes
1 answer

What is the use case for Access.BackingField in Fluent NHibernate?

The documentation for Access.BackingField() indicates that this: Sets the access-strategy to use the backing-field of an auto-property. I understand that auto-properties get compiled with backing fields, but if the property is by definition a…
Jay
  • 56,361
  • 10
  • 99
  • 123
3
votes
1 answer

How can I get class fields through reflection when backing fields are present?

I have a simple POCO class, e.g. class C { [MyAtrib] public int i {get; set;} [MyAtrib] public int i2; } When I call: GetType().GetFields( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); on that class (instance)…
igorludi
  • 1,519
  • 2
  • 18
  • 31
2
votes
1 answer

MethodBase as Hashtable Key

I want to store some backing fields of Properties declared in derived classes in protected Hashtable contained in base class. The usage of this mechanism in derived classes has to beas simple as possible. So, can I use MethodBase.GetCurrentMethod()…
Szybki
  • 1,083
  • 14
  • 29
1
2 3