0% found this document useful (0 votes)
98 views5 pages

Types in C#: Value Types (Byte, Int, Float, Bool, Char, Struct)

The document summarizes key concepts in C# including value types vs reference types, polymorphism through method overloading and overriding using virtual and override keywords, abstract classes and methods, interfaces, sealed classes and methods, and the differences between const and readonly. Value types are stored on the stack while reference types are stored on the heap. Method overriding allows calling an implementation from a derived class, while abstract classes define common functionality and force implementation in derived classes.

Uploaded by

Harsh Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views5 pages

Types in C#: Value Types (Byte, Int, Float, Bool, Char, Struct)

The document summarizes key concepts in C# including value types vs reference types, polymorphism through method overloading and overriding using virtual and override keywords, abstract classes and methods, interfaces, sealed classes and methods, and the differences between const and readonly. Value types are stored on the stack while reference types are stored on the heap. Method overriding allows calling an implementation from a derived class, while abstract classes define common functionality and force implementation in derived classes.

Uploaded by

Harsh Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Types in C#:

 Value types (byte, int, float, bool, char, struct)


o Value type variables are stored on stack.
o The variable of a value type contains the actual data on stack memory itself.
o When copied to another value type, whole data is copied.

STACK
int i = 123;
int j = i;
i
123
int i = 123;
j
123
int j = i;

 Reference types (String, Array, Custom classes, etc.)


o Reference type variable values are stored on heap.
o The reference type variables on stack are just the references of the data stored on heap.
o When copied to another reference type, only the reference of the data stored on the
heap is copied.

class Test
{
static void Main() {
int i = 10;
object o = i; // boxing
int j = (int) o; // unboxing
}
}
Polymorphsm

 Static (Method Overloading)


 Dyanamic (Method Overriding)

Method Overriding – using Virtual and Override keywords

Without using any keyword -


class A
{
public void Draw()
{
Console.WriteLine("A: Draw()");
}
}
class B : A
{
public void Draw()
{
Console.WriteLine("B: Draw()");
}
}

A a = new B();
a.Draw();

Output - A: Draw()

Compiler warning –
'B.Draw()' hides inherited member 'A.Draw()'. Use the new keyword if hiding was intended.

Using Virtual and Override – Allows us to invoke implementations of derived class methods from a base
class reference.

class A
{
public virtual void Draw()
{
Console.WriteLine("A: Draw()");
}
}
class B : A
{
public override void Draw()
{
Console.WriteLine("B: Draw()");
}
}
A a = new B();
a.Draw();

Output - B: Draw()

Abstract

Abstract methods are just declared without any implementation.


public abstract class Animal
{
protected string name;
public abstract string sound(); //all classes that implement Animal must have a sound
method
}

public class Cat : Animal


{
public Cat()
{
this.name = "Garfield";
}

override public string sound()


{ //implemented sound method from the abstract class & method
return "Meow!";
}
}

 An abstract class cannot be instantiated, but only extended.


 An abstract class can contain abstract members as well as non-abstract members.
 The class containing abstract members has to be abstract
 MUST implement all abstract members in the derived concrete class
 Abstract members cannot have private access modifier
 An abstract member cannot be static or virtual.
 Abstract classes cannot be a sealed class
 An abstract class cannot support multiple inheritance

Abstract class vs. Interface

Abstract class can have non-abstract (concrete) methods, while for interface; all methods have to be
abstract.

A class can inherit one or more interfaces, but only one abstract class.

Abstract class can have variables/fields, but interface can’t .


Interview Question – Can an abstract class have a constructor?

Ans – Yes

Abstract class constructor is used to initialize the fields of an abstract class.

Though we can not initialize an abstract class, an abstract class constructor is called
during the instantiation of derived class, just before derived class constructor.

Interview Question – Can we call abstract method from abstract class constructor?

Ans – Yes

It will be invoked when we instantiate a derived class. The abstract method’s overridden
version will be called.

Sealed

Applied to class – prevents derivation of the class

Applied to method – prevents overriding of methods in derived class

NOTE - If we want to declare a method as sealed, then it has to be declared as virtual in its base class.

The means only put sealed modifier on a method that overrides a virtual method of base class to
prevent further derived classes from overriding the specific implementation.

Interface
Const – compile time constant

We can only initialize a const variable to a compile time value, i.e., a value available to the compiler
while it is executing.

Once initialized at the time of declaration (compile-time), we cannot re-initialize a const variable.

NOTE - new() actually gets executed at runtime and therefore does not get value at compile time.
So this results in an error. So, const field of a reference type other than string can only be
initialized with null

A const variable cannot be marked as static.

ReadOnly – run time constant

We can initialize a ReadOnly variable to a compile-time value or a run-time value (new()).

Once initialized at compile-time or run-time, we cannot re-initialize a ReadOnly variable.

We cannot change the value of a readonly field except in a constructor.

You might also like