Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private Static Readonly Field Capitalization

People also ask

What is private static readonly in C#?

A Static Readonly type variable's value can be assigned at runtime or assigned at compile time and changed at runtime. But this variable's value can only be changed in the static constructor. And cannot be changed further. It can change only once at runtime.

What does private readonly mean?

If it's private and readonly , the benefit is that you can't inadvertently change it from another part of that class after it is initialized. The readonly modifier ensures the field can only be given a value during its initialization or in its class constructor.

What does readonly do in C#?

The readonly keyword can be used to define a variable or an object as readable only. This means that the variable or object can be assigned a value at the class scope or in a constructor only. You cannot change the value or reassign a value to a readonly variable or object in any other method except the constructor.

Can readonly be modified?

Because value types directly contain their data, a field that is a readonly value type is immutable. Because reference types contain a reference to their data, a field that is a readonly reference type must always refer to the same object. That object isn't immutable.


Do use Pascal casing in field names...

The naming guidelines for fields apply to static public and protected fields. You should not define public or protected instance fields.

MSDN Names of Type Members

Read-only static field = Pascal

MSDN Official Naming Convention

General Rules:

Do use Pascal casing for all public member, type, and namespace names consisting of multiple words.

Note that this rule does not apply to instance fields. For reasons that are detailed in the Member Design Guidelines, you should not use public instance fields.

Do use camel casing for parameter names.

Full List:

IDENTIFIER             CASE        EXAMPLE
----------------------+-----------+---------------
Class                  Pascal      AppDomain 
Enumeration types      Pascal      ErrorLevel 
Enumeration values     Pascal      FatalError 
Event                  Pascal      ValueChanged 
Exception class        Pascal      WebException 
Read-only static field Pascal      RedValue 
Interface              Pascal      IDisposable 
Method                 Pascal      ToString 
Namespace              Pascal      System.Drawing 
Parameter              Camel       typeName 
Property               Pascal      BackColor 

MSDN Official Naming Convention

Also consider

  • MSDN Guidelines for Names
  • MSDN General Naming Conventions

I got a warning in StyleCop for:

    private readonly Color LabelBackColor = Color.Black;

Warning 364 SA1306 : CSharp.Naming : Variable names and private field names must start with a lower-case letter: LabelBackColor.

See this discussion: private readonly fields should start with a lower case character. This means Camel case.


No, private static readonly is not in and of itself like const at all. Consider:

private static readonly IList<int> foo = new List<int>();

You can still do foo.Add(0);. Such fields are only const-like when the object itself, as well as any referenced objects, are immutable.


Here is an example reference source. Interestingly Microsoft here used underscore for private fields and camelCase (no underscore) for private static readonly http://referencesource.microsoft.com/#mscorlib/system/collections/arraylist.cs,3e3f6715773d6643. But this second example also shows, Microsoft doesn't have its own standard for field naming : http://referencesource.microsoft.com/#mscorlib/system/collections/hashtable.cs


I wouldn't worry about matching anyone else's conventions -- as long as you're consistent with whatever naming convention you use it doesn't really matter what you choose to do. There are far more important things to worry about.