Questions tagged [properties]

84 questions
59
votes
3 answers

Why does C# allow properties in interfaces?

In C#, the following code is valid interface I{ int property{get;set;} } Which doesn't make any sense to me. This seems to break one of the most important principles of interfaces: lack of state (in other words, no fields). Doesn't the…
36
votes
3 answers

Is it poor practice to name a property/member the same as the declaring type in C#?

For instance, a class like: class Dog { } //never mind that there's nothing in it... and then a property like: Dog Dog { get; set; } I've been told that if I can't come up with a more imaginative name for it, then I must use: Dog DogObject { get;…
LunchMarble
  • 463
  • 1
  • 4
  • 5
25
votes
4 answers

How do I handle setters on immutable fields?

I have a class with two readonly int fields. They are exposed as properties: public class Thing { private readonly int _foo, _bar; /// I AM IMMUTABLE. public Thing(int foo, int bar) { _foo = foo; …
kdbanman
  • 1,447
  • 13
  • 19
22
votes
9 answers

Should you use Fields or just Properties in C#?

I've seen some people who make properties for every single member, private or not... does this make any sense? private string mWhatever; private string Whatever { get { return this.mWhatever; } set { …
fordeka
  • 419
  • 1
  • 3
  • 10
18
votes
2 answers

Should I expose a "computed" value as a property or a method?

I have a C# class that represents a content type in a web content management system. We have a field that allows a web content editor to enter an HTML template for how the object is displayed. It basically uses the handlebars syntax for…
15
votes
5 answers

Best practices for handling large number of structured configuration/property files

Imagine a system that has a large number of servers. Each of them has a number of settings: Some specific to the server Some specific to the region Some common across all of them Maybe you can have some custom groupings, like this group of servers…
SDekov
  • 189
  • 1
  • 8
14
votes
3 answers

readonly vs. private getter-only property in C# 6

C# 6 added auto-property initializers and so we can do private List layouts1 { get; } = new List(); Is this better or worse than private readonly List layouts2 = new List(); (N.B. this is…
dumbledad
  • 317
  • 1
  • 2
  • 12
13
votes
3 answers

Why aren't properties implicitly convertible to delegates

We all know that properties in C# get compiled to actual plain old methods. But unlike method(-group)s, they can't be given as arguments to other methods that expect a delegate like a Func or Action (getter and setter). Is there anything…
sara
  • 2,549
  • 15
  • 23
13
votes
4 answers

How would I design an interface such that it's clear which properties may change their value, and which will remain constant?

I am having a design issue regarding .NET properties. interface IX { Guid Id { get; } bool IsInvalidated { get; } void Invalidate(); } Problem: This interface has two read-only properties, Id and IsInvalidated. The fact that they are…
stakx
  • 2,138
  • 18
  • 29
12
votes
5 answers

Is it a bad idea to use getters/setters and/or properties at all?

I am perplexed by comments under this answer: https://softwareengineering.stackexchange.com/a/358851/212639 A user is arguing there against the use of getters/setters and properties. He maintains that most times using them is a sign of a bad design.…
gaazkam
  • 3,517
  • 3
  • 19
  • 35
12
votes
7 answers

How did OOP evolve to include the notion of Properties

I've come from a C++ background and am going all out C# in my current job and I've just been reading a lot of Q&A about what's the difference between public fields and properties and all the back and forth in variations and incarnations of this…
jxramos
  • 359
  • 1
  • 9
10
votes
4 answers

Is guaranteeing immutability a justification for exposing a field instead of a property?

The general guidance for C# is to always use a property over a public field. This makes sense- by exposing a field, you're exposing a lot of implementation detail. With a property, you encapsulate that detail so it's hidden from consuming code, and…
Ben Aaronson
  • 6,883
  • 1
  • 30
  • 30
10
votes
5 answers

Best alternative of Property file in Java

Hey I an working on the product which is live at multiple portals. The product is developed in GWT, JAVA, Hibernate. My question is : Whether there is any alternative of using property file in java. My Requirement : For one property key there are…
Ranna
  • 359
  • 2
  • 3
  • 8
10
votes
2 answers

Property proper naming to represent string starting with number

Looking at some camera metadata on Windows File Properties there are (along with a few more) two Properties named Focal length and 35mm focal length. I'm developing software that will make use of these two Properties. So far I have created a…
iCantSeeSharp
  • 221
  • 1
  • 4
  • 11
10
votes
2 answers

JavaFX - the right way to use Properties with domain objects

JavaFX has provided a bunch of new Property objects, such as javafx.beans.property.DoubleProperty which allow you to define fields which can be automatically observed and synchronised. In many JFX examples, the MVC model class has a number of these…
pjm56
  • 101
  • 4
1
2 3 4 5 6