Thursday, December 28, 2017

Rules for naming variables in C#

In C#, the following rules are used for naming variables:

· A variable name must begin with a letter (a-z or A-Z) or an underscore (‘_’), which may be followed by a sequence of letters, digits (0-9), or underscores.

· The first character of a variable name cannot be a digit i.e. variable cannot starts with 0 to 9.

· A variable name must not contain any embedded spaces or symbols i.e. ? ! @ # + - % ^ & * ( ) [ ] { } . , ; : ″ ′ / and \.

· Instead of giving space to make a meaning, an underscore can be used wherever a space is required, like Highest_Marks.

· A variable name must be unique. For example, to store two different values, two unique variable names must be declared.

· A variable name can have any number of characters i.e. a variable name can be of any length.

· Keywords or reserved words cannot be used as variable names. For example, you cannot declare a variable named class as it is a keyword.

· C# is a case-sensitive language. It means that the Highest_Marks variable is not the same as the highest_marks variable. In other words, Uppercase letters are considered distinct from lowercase letters.

Examples of valid variable names are:

v  Error_Message

v  ResponseMessage

v  This_variable_name_is_very_long

v  _SerialNumber

Examples of invalid variable names are:

v  #count

v  2index

Share This
Previous Post
Next Post

TekGrabs, established in 2017, is in the business of sharing Information. It provides news, articles and photos about the different technologies, inventions in different domains. Provides information on programming and databases.