Reference data types store the memory reference of other variables. These other variable hold the actual values. Reference type can be classified as:
Object: Object is a built-in reference data type. It is a base class for all predefined and user-defined data types. A class is a logical structure that represents a real world entity. This means that the predefined and user-defined data types are created on the object class.
String: String is a built-in reference type. String type signifies Unicode character string values. Once string are created, they cannot be modified.
Class: A class is user-defined structure that contains variables and methods. For example, the Employee class can be a user-defined structure that can contain variables such as empsalary, empname, and empAddress. In addition, it can contain methods such as CalculateSalary() , which returns the net salary of an employee.
Delegate: A delegate is a user-defined reference type that stores the reference of one or more methods.
Interface: An interface is a type of user-defined class that is used for multiple inheritance.
Array: An array is user-defined data structure that contains values of the same data type, such as marks of students.
Variable Naming Rules
A variable needs to be declared before it can be referenced. You need to follow certain rules while declaring a variable:
- A variable name can begin with an upper case or a lower case letter. The name can contain letters, digits and the underscore character(_).
- The first character of the variable name must be a letter and not a digit. The underscore is also a legal first character, but it is not recommended at the beginning of a name.
- C# is a case-sensitive language; hence variable names count and count refer to two different variables.
- C# keywords cannot be used as variable names. If you still need to use a c# keyword prefix it with the ‘@’ symbol.
Note: Microsoft recommends camelcase notation for c# variable names. You should not use underscores and must ensure that the first letter of the identifier is in lowercase. In addition, you must capitalize the first letter of each subsequent word of the identifier. For example, consider the following variable declarations:
Int totMonths = 12;
String empName = “John Ferb”;
Bool statusInfo = true;
Create and use variables
A variable’s type and identifier (name ) need to be mentioned when you declare a variable. This tells the compiler the name of the variable and the variable and the type of data the variable will store. If you attempt to use an undeclared variable, the compiler will generate and error message. The table given below displays a list of valid and invalid variable names in c#.
Invalid as it is a keyword
Invalid as a variable cannot start with a digit
Invalid as a variable cannot have the special character &
Note: When you declare a variable, the computer allocates memory for it. Hence, to avoid wasting computer memory, it is recommended to declare variables only when required.