0

I want to create private list, while making it public as readonly\unwriteable, unable to add\remove\insert cells\nodes to the list.

My current solution is:

public IEnumerable<DataToken> DataTokens {
    get {
        return from v in _datatokens select v;
    }
}
private List<DataToken> _datatokens;

I know the property's name is fine, but what about the variable's name? I can't name it dataTokens because it's parameter's name's format. Currently I call it _datatokens that is really bad name.

Is there a better alternative? I couldn't find anything about it at msdn.

KugBuBu
  • 630
  • 5
  • 21
  • 1
    `dataTokens` and `DataTokens` are two different identifiers in C#. You could name your field `dataTokens`. – Dirk Sep 23 '14 at 11:47
  • 1
    That is not a "really bad name" and this is opinion-based. – CodeCaster Sep 23 '14 at 11:48
  • @Dirk It's parameter's name's format. This isn't corrent. – KugBuBu Sep 23 '14 at 11:49
  • @CodeCaster In C++ where screaming constant are there, _ is bad. – KugBuBu Sep 23 '14 at 11:49
  • What does _"It's parameter's name's format"_ mean? Can you phrase that differently or show relevant code? And what are screaming constants? Anyway prefixing private fields with underscores is pretty common in C#. – CodeCaster Sep 23 '14 at 11:50
  • 2
    Someone can cast the result to a `List` and then it wouldn't be readonly anymore... – Dennis_E Sep 23 '14 at 11:52
  • @CodeCaster `DataType parameterType, DataType returnType` this is the correct way to type parameters, look at their names, they start with non-capital letter. C++ has `I_MAKE_HORROR` names. – KugBuBu Sep 23 '14 at 11:53
  • Why is `_datatokens` a 'really bad name'? I quite often name backing fields using the same convention. What's stopping you from using `dataTokens` as the field name anyway? – DGibbs Sep 23 '14 at 11:53
  • @Dennis_E Just tested it, I will rename it to `ReadOnlyCollection`. – KugBuBu Sep 23 '14 at 11:56
  • @DGibbs I am a bit of a nazi grammar in my programming. – KugBuBu Sep 23 '14 at 11:57
  • possible duplicate of [C# Field Naming Guidelines?](http://stackoverflow.com/questions/3186853/c-sharp-field-naming-guidelines) – Patrice Gahide Sep 23 '14 at 12:02

2 Answers2

2

Since identifiers are case-sensitive in C#, there is nothing against datatokens as a variable name.

There is a guideline available on MSDN that would suggests it to be dataTokens (camel-cased).

By the way, you should use ReadOnlyCollection<T> as return type to make it read-only to the outside world, so I would suggest to split this one up in two properties with different access modifiers.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

C# also supports auto-properties, so you can save yourself the trouble of solving this conundrum and simply declare your property thus;

public IEnumerable<DataToken> DataTokens { get; private set; }
RJ Lohan
  • 6,497
  • 3
  • 34
  • 54