automatic-properties Questions

15

Solved

We're often told we should protect encapsulation by making getter and setter methods (properties in C#) for class fields, instead of exposing the fields to the outside world. But there are many tim...
Graphic asked 25/7, 2009 at 1:4

5

Solved

I follow a pattern when setting certain properties whereby I check to see if the corresponding field is empty, returning the field if not and setting it if so. I frequently use this for reading con...
Furnish asked 17/8, 2015 at 13:43

5

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? ...
Cowled asked 18/12, 2013 at 18:12

14

Solved

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 p...
Deport asked 27/10, 2010 at 19:17

23

Solved

How do you give a C# auto-property an initial value? I either use the constructor, or revert to the old syntax. Using the Constructor: class Person { public Person() { Name = "Initial Name...
Glyoxaline asked 2/9, 2008 at 21:29

6

Solved

This might sound naive, but... class Widget { public int Foo { get; set; } } That's cool, and saves some boilerplate against using a backing field, but at that point, isn't it equivalent to sim...
Fontana asked 22/2, 2011 at 3:41

11

Solved

Could someone provide a very simple explanation of Automatic Properties in C#, their purpose, and maybe some examples? Try to keep things in layman's terms, please!
Cotoneaster asked 14/5, 2011 at 12:45

5

Solved

I was told that in c# attributes are not allowed on the auto-implemented properties. Is that true? if so why? EDIT: I got this information from a popular book on LINQ and could not believe it! EDI...
Gracchus asked 21/1, 2009 at 11:21

3

When you implement the INotifyPropertyChanged interface, you're responsible for calling the PropertyChanged event each and everytime a property is updated in the class. This typically leads ...

2

I have found myself cornered, so here we go. Context I need to produce a fingerprint hash code for object diffing. Comparing the hashes of two sets of objects will need to tell me if there are id...

8

Solved

For C#, I hate writing out the variables and then writing out all the properties. Isn't there a way to select all variables, right click and create all the properties.
Kislev asked 27/4, 2010 at 19:20

6

Solved

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: ...
Landholder asked 25/5, 2011 at 13:54

5

Solved

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 f...
Diaconal asked 6/10, 2011 at 6:8

2

Solved

I tried the following example: public class TestBase { public virtual string ReadOnly { get; } public TestBase() { ReadOnly = "from base"; } } class Test : TestBase { public override strin...
Sandglass asked 30/11, 2018 at 10:49

6

Solved

I recently found out about auto-properties and like them quite a lot. At this moment I am trying to use them everywhere where I can. Not really to just be able to use them everywhere, but more to s...
Talishatalisman asked 22/1, 2010 at 12:46

4

Solved

I want to do something like this: class Foo { bool Property { get; set { notifySomethingOfTheChange(); // What should I put here to set the value? } } } Is there anything I can put ther...
Letreece asked 10/7, 2012 at 13:11

10

Solved

I realize that it seems to be a duplicate of What is the difference between a Field and a Property in C#? but my question has a slight difference (from my point of view): Once I know that I will...
Spelt asked 17/3, 2009 at 9:35

3

When I have Visual Studio 2017 generate properties for me, it will always use the new expression-bodied properties, for example: private static string username; internal static string Userna...
Ciccia asked 18/5, 2017 at 10:11

2

Solved

I have the following code class: public class Foo { public Nested Bar { get; } = new Nested(this); public class Nested { public Nested(Foo foo) { foo.DoSomething(); } } private void DoS...
Nunuance asked 15/5, 2017 at 10:30

5

Solved

I have a property which is currently automatic. public string MyProperty { get; set; } However, I now need it to perform some action every time it changes, so I want to add logic to the setter. ...
Hike asked 23/5, 2016 at 13:25

6

Solved

What's the easiest/straight-forward way of setting a default value for a C# public property? // how do I set a default for this? public string MyProperty { get; set; } Please don't suggest that...
Casie asked 26/7, 2010 at 14:11

8

Solved

I have a property: public Dictionary<string, string> MyProp { get; set; } When I invoke that property to add an item, I get a NullReferenceException. How would I do the null check in the pro...
Cohobate asked 10/4, 2011 at 17:2

2

Solved

In C# 6.0, the new syntax let us write read-only auto-properties with using an initializer: public bool AllowsDuplicates { get; } = true; Likewise, we can write it using an expression body gett...
Picked asked 2/5, 2016 at 20:36

3

Solved

I need to create and manage many simple published properties. I call them auto-properties if they look like that: private FTitle: string; published property Title: string read FTitle write FTitl...
Terrazas asked 30/5, 2012 at 12:12

1

I want to disable automatic id creation in Django Models. Is it possible to do so? How?
Wigley asked 17/10, 2015 at 15:49

© 2022 - 2024 — McMap. All rights reserved.