You are on page 1of 12

Step By Step C# Tutorial

Data Type
Data Type refers to the type of data that can be stored in a variable. It also specifies how much memory
would be allocated to a variable and the operations that can be performed on that variable.
C# is rich in data type which is broadly divided into two categories.
1.

Value Type

2.

Reference Type

Value Type
A value type variable store actual values. Also, value types are stored in a stack. Values types are of two
types - built-in and user-defined. Value types are derived from System.ValueType.

Reference Type
A reference type variable stores a reference to the actual value. It means reference type contains a pointer
to another memory location that holds the actual data. Also, reference types are stored in a heap. Reference
types are of two types - built-in and user-defined. Reference types are derived from System.Object.

1.

What is Nullable Type?


Value types (like int, bool etc.) which accept either their normal values or a null value are referred as Nullable
types. Nullable types are instances of the Nullable struct.
For example, A Nullable<bool> is pronounced as "Nullable of bool," and it can accept the values true, false or null.
Nullable types can also be defined using ? type modifier. This token is placed immediately after the value type
being defined as nullable.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

//assigning normal value


Nullable flag = true;
//OR
bool? flag = true;
//assigning normal value
Nullable x = 20;
//OR
int? x = 20;

2. When to use Nullable Type?


When you deal with databases and other data types which contain elements or variables that may or not be
assigned a value i.e. undefined, nullable types are used to store the values of such elements or variables.

3. Can you define string as Nullable Type?


You cant declare a string as nullable because, it is implicitly nullable. Hence, declaring a string variable like string?
strName results a compile time error.

Static
The static keyword is used to specify a static member, which means static members are common to all the
objects and they do not tied to a specific object. This keyword can be used with classes, fields, methods,
properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types
other than classes.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

class MyClass

static int X = 10;


int Y = 20;
public static void Show()

Console.WriteLine(X);
Console.WriteLine(Y); //error, since you can access only static members

}
}

Key points about Static keyword


1.

If the static keyword is applied to a class, all the members of the class must be static.

2.

Static methods can only access static members of same class. Static properties are used to get or
set the value of static fields of a class.

3.

Static constructor can't be parameterized. Access modifiers can not be applied on Static constructor,
it is always a public default constructor which is used to initialize static fields of the class.

Different Types of Type Casting or Type Conversion


1.

Implicit conversion
Implicit conversion is being done automatically by the compiler and no data will be lost. It includes
conversion of a smaller data type to a larger data types and conversion of derived classes to base class. This
is a safe type conversion.
1. int smallnum = 654667;
2. // Implicit conversion
3. long bigNum = smallnum;
4. class Base
5. {
6.

public int num1 { get; set; }

7. }
8.
9. class Derived : Base
10. {
11. public int num2 { get; set; }
12. }
13.
14. class Program
15. {
16. static void Main(string[] args)
17. {
18. Derived d = new Derived();
19. //Implicit Conversion
20. Base b = d;
21. }
22. }

2. Explicit conversion
Explicit conversion is being done by using a cast operator. It includes conversion of larger data type to
smaller data type and conversion of base class to derived classes. In this conversion information might be
lost or conversion might not be succeed for some reasons. This is an un-safe type conversion.
1. long bigNum = 654667;
2. // Explicit conversion
3. int smallnum = (int)bigNum;

4. class Base
5. {
6.

public int num1 { get; set; }

7. }
8.
9. class Derived : Base
10. {
11. public int num2 { get; set; }
12. }
13.
14. class Program
15. {
16. static void Main(string[] args)
17. {
18. Base b = new Base();
19. //Explicit Conversion
20. Derived d = (Derived)b;
21. }
22. }

Understanding virtual, override and new keyword in


C#
As you know Polymorphism is the concepts of OOPS which includes method overriding and method
overloading. Virtual and Override keyword are used for method overriding and new keyword is used for
method hiding. Let's have look on these keywords in C# and try to understand each importance.

Simple Class Inheritance


Consider the below class hierarchy with classes A, B and C. A is the super/base class, B is derived from class
A and C is derived from class B.

If a method Test() is declared in the base class A and classes B or C has no methods as shown below.
1. using System;
2. namespace Polymorphism
3. {
4.

class A

5.

6.

public void Test() { Console.WriteLine("A::Test()"); }

7.

8.
9.

class B : A { }

10.
11. class C : B { }
12.
13. class Program

14. {
15. static void Main(string[] args)
16. {
17. A a = new A();
18. a.Test(); // output --> "A::Test()"
19.
20. B b = new B();
21. b.Test(); // output --> "A::Test()"
22.
23. C c = new C();
24. c.Test(); // output --> "A::Test()"
25.
26. Console.ReadKey();
27.
28. }
29. }
30. }
Suppose you have Test() method in all the classes A, B, C as shown below:
1. using System;
2. namespace Polymorphism
3. {
4.

class A

5.

6.

public void Test() { Console.WriteLine("A::Test()"); }

7.

8.
9.

class B : A

10. {
11. public void Test() { Console.WriteLine("B::Test()"); }
12. }
13.
14. class C : B
15. {
16. public void Test() { Console.WriteLine("C::Test()"); }
17. }
18.
19. class Program
20. {
21. static void Main(string[] args)
22. {
23.
24. A a = new A();
25. B b = new B();
26. C c = new C();
27.
28. a.Test(); // output --> "A::Test()"

29. b.Test(); // output --> "B::Test()"


30. c.Test(); // output --> "C::Test()"
31.
32. a = new B();
33. a.Test(); // output --> "A::Test()"
34.
35. b = new C();
36. b.Test(); // output --> "B::Test()"
37.
38. Console.ReadKey();
39. }
40. }
41. }
When you will run the above program, it will run successfully and gives the O/P. But this program will show
the two warnings as shown below:
1.

'Polymorphism.B.Test()' hides inherited member 'Polymorphism.A.Test()'. Use the new keyword if


hiding was intended.

2.

'Polymorphism.C.Test()' hides inherited member 'Polymorphism.B.Test()'. Use the new keyword if


hiding was intended.

Method Hiding (new keyword)


As you have seen in the above example the compiler generate the warnings since C# also supports method
hiding. For hiding the base class method from derived class simply declare the derived class method with
new keyword. Hence above code can be re-written as :
1. using System;
2. namespace Polymorphism

3. {
4.

class A

5.

6.

public void Test() { Console.WriteLine("A::Test()"); }

7.

8.
9.

class B : A

10. {
11. public new void Test() { Console.WriteLine("B::Test()"); }
12. }
13.
14. class C : B
15. {
16. public new void Test() { Console.WriteLine("C::Test()"); }
17. }
18.
19. class Program
20. {
21. static void Main(string[] args)
22. {
23.
24. A a = new A();
25. B b = new B();

26. C c = new C();


27.
28. a.Test(); // output --> "A::Test()"
29. b.Test(); // output --> "B::Test()"
30. c.Test(); // output --> "C::Test()"
31.
32. a = new B();
33. a.Test(); // output --> "A::Test()"
34.
35. b = new C();
36. b.Test(); // output --> "B::Test()"
37.
38. Console.ReadKey();
39. }
40. }
41. }
Moreover, if you are expecting the fourth and fifth output should be "B::Foo()" and "C::Foo()" since the
objects a and b are referenced by the object of B and C respectively then you have to re-write the above
code for Method Overriding.

Method Overriding (virtual and override keyword)


In C#, for overriding the base class method in derived class, you have to declare base class method
as virtual and derived class method as override as shown below:
1. using System;
2. namespace Polymorphism

3. {
4.

class A

5.

6.

public virtual void Test() { Console.WriteLine("A::Test()"); }

7.

8.
9.

class B : A

10. {
11. public override void Test() { Console.WriteLine("B::Test()"); }
12. }
13.
14. class C : B
15. {
16. public override void Test() { Console.WriteLine("C::Test()"); }
17. }
18.
19. class Program
20. {
21. static void Main(string[] args)
22. {
23.
24. A a = new A();
25. B b = new B();

26. C c = new C();


27. a.Test(); // output --> "A::Test()"
28. b.Test(); // output --> "B::Test()"
29. c.Test(); // output --> "C::Test()"
30.
31. a = new B();
32. a.Test(); // output --> "B::Test()"
33.
34. b = new C();
35. b.Test(); // output --> "C::Test()"
36.
37. Console.ReadKey();
38. }
39. }
40. }

Note
1.

The virtual keyword is used to modify a method, property, indexer, or event declared in the base
class and allow it to be overridden in the derived class.

2.

The override keyword is used to extend or modify a virtual/abstract method, property, indexer, or
event of base class into derived class.

3.

The new keyword is used to hide a method, property, indexer, or event of base class into derived
class.

You might also like