Illustration of Operator Keywords (new, sizeof) with Examples in C# (C Sharp)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 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
{ 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 dont 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:
Here is an example to demonstrate the usage of new constraint in a generic class: class sampleGeneric<T>
where T : new() { 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 { Output of this code will be: Invoking
testMethod of derivedClass 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
{ Output of this code will be: Size of intVar
is 4 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
{ Output of this code will be: Size of int
is 4 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.
|