VariablesA variable represents a strong location. Each variable has a type that determines what values can be stored in the variable. A variable must definitely be assigned before its value can be obtained.In C#, you declare a variable in this format:[modifiers] datatype identifier;In this case, the modifier is an access modifier. The "variable Modifiers" section will discuss class member access modifiers. The data type refers to the type of value a variable can store. The identifier is the name of variable.The next two examples are declarations of variable where public is the modifier, int is the data type, and num1 is the name. The second variable type is a local variable. A local variable can't have modifier because it sits inside a method and is always private to the method. Here are the examples:public int num1;
and:int num1;A value can be assigned to variable after it's declared. You can also initialize a value during a variable declaration. For example:int num1 = new Int16();num1 = 34;int num2 = 123;
Variable Modifiers Modifiers enable you to specify a number of features that you apply to your variable. You apply a variable modifier when you declare a variable. Keep in mind that mo-differs can be applied to fields not to local variables.Note: A local variable only has scope within its defined block in the program. A variable can have one or combination of more then one of the following types: internal, new, private, public, protected, read only, and static. Accessibility modifiersSome of the modifiers discussed in previous sections can set the accessibility level of variables. These are called accessibility modifiers (see table 1-7).Table 1-7. Accessibility modifiers
You'll now examine access modifiers in an example. In listing 1-21, AccessCls is a class accessed by the Main method. The Main method has access to num1 because it's defined as a public variable, but not to num2 because it's a private variable.Listing 1-21 variable access modifiers.using System;class VarAccess{ class AccessCls { public int num1 = 123; int num2 = 54; } static void Main() { AccessCls cls = new AccessCls(); int num1 = 98; num1 = cls.num1; //int i = cls. Num2; Console.WriteLine(num1.ToString()); }}
When you access class members, the num2 variable is not available in the list of its members. See figure 1-6.Figure 1-6. Available members of AccessClsIf you try access num2 from the main program, the compiler gives the error shown in figure 1-7Figure 1-7 error given when trying to access a private member of classStatic and Read-Only VariablesBy default, a field is an instance field. That means a new copy of variable is creates for each instance of the class to which it belongs. There are some cases where you want the variable to be shared by ever instance of the class, and it's in such cases that static fields are useful. By defining the static keyword, you can restrict a field to create only one instance of the variable of a class and share it with all other class instance of the same type. In other words, if you change the value of a static variable in a class, all instance at the class level rather then the instance level. You can use the static modifier alongside other modifiers.For example:public static int num2 = 34;You can modify the value of a variable once it's initialized, but there are some cases where you don't want to change the value of the variable after it's assigned during initialization. In these cases, you can the read -only modifier to prevent modification.Constants Constants are similar to read-only fields. You can't change a constant value once it's assigned. The const keyword precedes the field to define it as a constant. Assigning value to a constant would give a compilation error. For example:const int num3 = 34;num3 = 54;// Compilation error: the left-hand side of an assignment must// be a variable, property or indexerAlthough constant are similar to read-only fields, some differences exist. You can also declare local variables to be constants. Constants are always static, even though you don't use the static keyword explicitly, so they're shared by all instances of the class.