Step by Step C# Tutorial: Data Type
Step by Step C# Tutorial: Data Type
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.
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
Console.WriteLine(X);
Console.WriteLine(Y); //error, since you can access only static members
}
}
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.
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.
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.
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. }
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.
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.
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()"
2.
3. {
4.
class A
5.
6.
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();
3. {
4.
class A
5.
6.
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();
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.