Attributes enable the programmer to give certain declarative information to the elements in their class. These elements include the class itself, the methods, the fields, and the properties. You can choose to use some of the useful built-in attributes provided with the .NET platform, or you can create your own. Attributes are specified in square brackets ( [. . .] ) before the class element upon which they’re implemented. Table 6 shows some useful attributes provided with .NET.
Table 6 Useful Built-in Attributes
NAME | DESCRIPTION | EXAMPLE |
DllImport | Imports a native DLL | [DllImport(“winmm.dll”) ] |
Serializable | Makes a class serializable | [Serializable] |
Conditional | Includes/omits a method based on condition | [Conditional(Diagnostic”)] |
| | |
8. Variables
A 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 modifiers
Some of the modifiers discussed in previous sections can set the accessibility level of variables. These are called accessibility modifiers (see table 7).
Table 7. Accessibility modifiers
MODIFIER | DESCRIPTION |
internal | The variable can only accessed by the current program. |
public | The variable can be accessed from any where as a field. |
protected | The variable can only be accessed with the class in which it’s defined and it’s derived class. |
protected internal | The variable can only be accessed from the current program and the type derived from the current program. |
private | The variable can only be accessed within the type in which it’s defined. |
| |
You’ll now examine access modifiers in an example. In listing 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 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 6.
Figure 6. Available members of AccessCls
If you try access num2 from the main program, the compiler gives the error shown in figure 7
Figure 7. Error given when trying to access a private member of class
Static and Read-Only Variables
By 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.
No comments:
Post a Comment