C# - Attributes & Variables & Constants

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 below shows some useful attributes provided with .NET.

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;

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).

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.

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 i

nstance 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

Consants 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 consant would give a compilation error. For eg:

const int num5 = 34;

num5 = 54;

//Compilation error: the left-hand side of an assignement must be a variable, property or indexer

although constants are similar to read-only fields, some differnces exist. You can also declare local variables to be consants. Consants are always static, even though you don' use the static keyword, so the're shared by all instances of the class.