• No results found

Sealed Classes

In document Basic c# (Page 134-139)

Sealed is another modifier that applies to classes. aaa is a sealed class. No class can derive from aaa. In another words aaa cannot act as a base class for any class.

a.cs

public class zzz {

public static void Main() {

} }

sealed class aaa {

}

class bbb : aaa {

}

Compiler Error

a.cs(10,7): error CS0509: ‘bbb’ : cannot inherit from sealed class ‘aaa’

a.cs

public class zzz {

public static void Main() {

aaa a = new aaa();

System.Console.WriteLine(a.i);

a.abc();

} }

sealed class aaa {

public int i = 9;

public void abc() {

System.Console.WriteLine(“hi”);

} } Output 9 hi

The only difference between a sealed class and a non-sealed class is that a sealed class cannot be derived from. Otherwise there is no difference at all. It can contain the same variables, functions etc as a normal class does . A sealed class lets us create classes which no one can derive from. Thus the code in such classes cannot be overridden. Also as the compiler knows this, certain run time optimizations can be performed on a sealed class

Constants

a.cs

public class zzz {

const int i = 10;

public static void Main() { System.Console.WriteLine(i);

} } Output 10

A constant or const variable behaves as a variable. We give it an initial value and can use it wherever we can use a variable.

a.cs

public class zzz {

const int i = 10;

public static void Main() {

i++;

System.Console.WriteLine(i);

i = 30;

} }

Compiler Error

a.cs(6,1): error CS0131: The left-hand side of an assignment must be a variable, property or indexer

a.cs(8,1): error CS0131: The left-hand side of an assignment must be a variable, property or indexer

Unlike a variable, we are not allowed to change the value of a const. The change is an assignment statement.

a.cs

public class zzz {

const int i ;

public static void Main() {

i = 30;

System.Console.WriteLine(i);

} }

Compiler Error

a.cs(3,13): error CS0145: A const field requires a value to be provided

We have to initialize the const variable at the time of creation. We are not allowed to initialize it later in our program.

a.cs

public class zzz {

const int i = j + 4;

const int j = k - 1;

const int k = 3;

public static void Main() {

System.Console.WriteLine(“{0} {1} {2}”,i,j,k);

} }

Output 6 2 3

A constant can depend upon another constant. C# is smart enough to realize that to calculate the value of const i, it first needs to know the value of j. j’s value depends upon another const k, whose value is 3.

Thus C# first evaluates k to 3 then j becomes 2 i.e. k -1 and finally i takes on the value of j i.e. 2 + 4 resulting in 6.

Like classes const’s cannot be circular i.e., they cannot depend upon each other.

a.cs

public class zzz {

const int i = j + 4;

const int j = k - 1;

const int k = i;

public static void Main() {

System.Console.WriteLine(“{0} {1} {2}”,i,j,k);

} }

Compiler Error

a.cs(3,11): error CS0110: The evaluation of the constant value for ‘zzz.i’ involves a circular definition

The value of the const i depends upon j which in turn depends upon k, which is equal to i. This becomes a circular definition. A const is a variable whose value cannot be changed but whose initial value is compile time determined.

a.cs

public class zzz {

public const aa a = new aa();

public static void Main() {

} }

public class aa {

}

Compiler Error

a.cs(3,17): error CS0133: The expression being assigned to ‘zzz.a’ must be constant a.cs

public class zzz {

public const aa a = null;

public static void Main() {

} }

public class aa {

}

The error vanishes as we are now initializing a to an object which has a value that can be determined at compile time. We cannot ever change the value of a, so it will always be null. Normally we do not have consts as a reference type as they have value only at runtime.

As mentioned earlier we can only initialize a const to a compile time value i.e. a value available to the compiler while it is executing. new unfortunately gets executed at runtime and therefore has no value at compile time. This gives us an error.

a.cs class zzz {

public static void Main() {

yyy y = new yyy();

System.Console.WriteLine(y.i);

} }

class yyy {

public const int i = 3;

}

Compiler Error

a.cs(6,26): error CS0176: Static member 'yyy.i' cannot be accessed with an instance reference;

qualify it with a type name instead

A constant is static by default and we cannot use the instance reference i.e. a name to reference a const.

A const has to be static as no one is allowed to make any changes to a const.

a.cs class zzz {

public static void Main() {

} }

class yyy {

public static const int i = 3;

}

Compiler Error

a.cs(9,25): error CS0504: The constant ‘yyy.i’ cannot be marked static

C# does not want us to repeat the obvious over and over again. Just like humans, programming language too have their own quirks. Some other time, perhaps, C# may permit us to write a static before an entity that is already static by default.

a.cs class zzz {

public static void Main() {

System.Console.WriteLine(yyy.i + “ “ + xxx.i);

} }

class yyy {

public const int i = 3;

}

class xxx : yyy {

public const int i = 30; } Compiler Warning

a.cs(14,18): warning CS0108: The keyword new is required on ‘xxx.i’ because it hides inherited member ‘yyy.i’

Output 3 30

We can create a const with the same name as another const in the base class. The const of the class xxx i will hide the const i in class yyy for the class xxx only.

Fields

A field to start with is another word for a variable in a class. There are a large number of generic rules that apply to all members of a class and we will not tire you by repeating them ad nauseam.

A variable can never have an uninitialized value in C#.

a.cs

public class zzz {

static int i;

static bool j;

public static void Main() {

System.Console.WriteLine(zzz.i + “ “ + zzz.j );

} } Output 0 False

Static variables are initialized when the class is loaded first. An int is given an initial value of zero and a bool False.

a.cs

public class zzz {

int i;

bool j;

public static void Main() {

zzz a = new zzz();

System.Console.WriteLine(a.i + “ “ + a.j );

} } Output 0 False

An instance variable is initialized at the time of creation. The keyword new will create an instance of the zzz. It will allocate memory for each of the non static variables and then initialize each of them to their default values.

a.cs

public class zzz {

static int i = j + 10;

static int j = i + 1;

public static void Main() {

System.Console.WriteLine(zzz.i + “ “ + zzz.j );

} } Output 10 11

Outputs make a lot of sense if you understand them in plain simple English. C# always initializes static fields to their initial value after creating them . Variables i and j are thus given a default of zero. Then C# realizes that these variables need to be assigned some values. It does not read all the lines, only one at a time. It will now read the first line and as the variable j has a value of 0, i will get a value of 10.

Then at the next line, j is the value of i plus 1. The variable i has a value of 10 and j now becomes 11. As it does not see both lines at the same time, it does not notice the circularity of the above definition. In short, though the above example works, it is frowned upon by the powers to be at C#.

a.cs

public class zzz {

int i = j + 10;

int j = i + 1;

public static void Main() {

} }

Compiler Error

a.cs(3,9): error CS0236: A field initializer cannot reference the nonstatic field, method, or property ‘zzz.j’

a.cs(4,9): error CS0236: A field initializer cannot reference the nonstatic field, method, or property ‘zzz.i’

It does not work for instance variables as the rules of an instance variable are different than that of static.

The field initializer of an instance variable has to be determined at the time of creation of the object. The variable j does not have a value at this point in time. It cannot refer to variables of the same instance at the time of creation. Thus we can refer to no instance members to initialize an instance member. Textual order means first come first served.

In document Basic c# (Page 134-139)