Illustration of Operator Keywords and examples

Illustration of Operator Keywords (new, sizeof) with Examples in C#

In this article, you will analyze the purpose and usage of new and sizeof operator keywords.

Operator Keyword new:

The operator keyword new can be used in three different forms. They are:

· new operator

· new constraint

· new modifier

You will understand the difference between each of these with the help of the following examples.

Overview of new Operator:

The new operator is one which you commonly use for instantiating objects of a class. While instantiating a class, you can also invoke the appropriate constructors using new operator. Here is an example:

class sampleClass {

int classVar;

public sampleClass(){

classVar = 10;

}

public sampleClass(int data) {

classVar = data;

}

}

class testClass {

public static void Main(){

sampleClass obj1 = new sampleClass();

sampleClass obj2 = new sampleClass(50);

Console.WriteLine(“obj1.classVar = {0}, obj2.classVar = {1}”, obj1.classVar,

obj2.classVar);

}

}

Output of this code will be:

obj1.classVar =10, obj2.classVar =50

In this example, you use new operator to instantiate sampleClass and create objects obj1 and obj2. When creating obj1 you have called the no-argument constructor and while creating obj2 you have called one argument constructor by passing the value 50. Even if you don't explicitly specify no-argument constructor, default constructor will be triggered.

Overview of new Constraint:

The new constraint is used along with generic class to enforce the following constraint:

“Type that is passed as an argument to the generic class should compulsorily have a no-argument constructor with access modifier as public.”

Here is an example to demonstrate the usage of new constraint in a generic class:

class sampleGeneric<T> where T : new() {

public T testMethod() {

return new T();

}

}

Overview of new Modifier:

When derived class defines a method that already exists in base class, you say that the derived class method overrides the base class method meaning that the derived class method extends the implementation code of base class method. But there are situations when the derived class method is a brand new method independent of base class method. However they both have the same name. In such case, you can hide the base class method in the derived class using new modifier. Here is an example showing the usage of new modifier:

public class baseClass {

public static void testMethod(){

Console.WriteLine(“Invoking testMethod of baseClass”);

}

}

public class derivedClass : baseClass {

new public static void testMethod(){

Console.WriteLine(“Invoking testMethod of derivedClass”);

}

public static void Main(){

testMethod();

baseClass.testMethod();

}

}

Output of this code will be:

Invoking testMethod of derivedClass

Invoking testMethod of baseClass

In this example, both baseClass and derivedClass have a method with same name called testMethod. It is not an overridden method. Hence in the derived class testMethod declaration, you include the keyword new. Now the testMethod of baseClass is hidden. You can still access baseClass method from derivedClass by invoking it as baseClass.testMethod().

Operator Keyword sizeof:

When you declare a value type, set of bytes will be allocated to it. Number of bytes allocated to each value type variable you use can be determined using sizeof keyword. Here is an example to demonstrate usage of this keyword:

class sampleClass {

public static void Main(){

int intVar = 10;

Console.WriteLine(“Size of intVar is {0} bytes”, sizeof(intVar));

double doubleVar = 12.5;

Console.WriteLine(“Size of doubleVar is {0} bytes”, sizeof(doubleVar));

}

}

Output of this code will be:

Size of intVar is 4 bytes

Size of doubleVar is 8 bytes

It means that 4 bytes are allocated for integer and 8 bytes are allocated for double. You can directly apply sizeof on the value types rather than its variables. Here is an example using sizeof on value types directly:

class sampleClass {

public static void Main(){

Console.WriteLine(“Size of int is {0} bytes”, sizeof(int));

double doubleVar = 12.5;

Console.WriteLine(“Size of double is {0} bytes”, sizeof(double));

}

}

Output of this code will be:

Size of int is 4 bytes

Size of double is 8 bytes

Similarly every other value type has a specific number of bytes allocated to it, which can be identified using sizeof keyword. Not just value types, sizeof keyword can also be used on Enum types, User-defined structs and pointer types.

References:

http://msdn.microsoft.com

www.codeguru.com

www.dotnetperls.com

Please be aware that the free essay that you were just reading was not written by us. This essay, and all of the others available to view on the website, were provided to us by students in exchange for services that we offer. This relationship helps our students to get an even better deal while also contributing to the biggest free essay resource in the UK!