Sunteți pe pagina 1din 27

Session 4

Structures
Enumeration
Garbage Collection
Nested Classes
Abstract Classes
Interfaces

Structures (struct)
Defining a struct
Instantiating the struct

Structures (struct)
Structures, denoted in C# by the struct keyword, are
lightweight objects. Structures are very similar to
classes in C#
A struct is useful for creating types that are used to
hold data like Point, Rectangle, Color types.
A struct is of value type, contrary to classes which are
of reference type
Instances of a struct can be created with and without
using the new keyword.

Instantiating the struct


A struct can be instantiated in three ways:
Using the new keyword and calling the default
no-argument constructor
Using the new keyword and calling a custom
or user defined constructor.
Without using the new keyword

Example program using struct


public struct structure
{
int rollno;
string name;
int m1;
int m2;
double avg;
int total;
public void structureread(int no, string na, int ma1, int ma2)
{
rollno = no;
name = na;
m1 = ma1;
m2 = ma2;
total = m1 + m2;
avg = total / 2;
}

public void print()


{
Console.WriteLine("rollno " + rollno);
Console.WriteLine("name " + name);
Console.WriteLine("m1 " + m1);
Console.WriteLine("m2 " + m2);
Console.WriteLine("total " + total);
Console.WriteLine("avg " + avg);
}
}

struct mainstructure
{
public static void Main()
{
structure s1 = new structure();
structure s2 = new structure();
s1.structureread( 101, "SUCHI" , 30, 50);
s2.structureread( 102, "SMITA", 20, 70);
s1.print();
s2.print();
Console.ReadLine();
}
}

Enumeration
The Need for Enumeration
Using Enumeration (enum)
More about Enumerations

Enumeration
Enumerations, like classes and structures, also
allow us to define new types. Enumeration
types contain a list of named constants. The
list is called an enumerator list here while its
contents are called enumerator identifiers.
Enumerations are of integral types like int,
short, byte, (except the char type).

The Need for Enumeration


Write notes here

Using Enumeration (enum)


Enumerations are defined in C# using the
enum keyword. The enumerator identifiers
(named constants) are separated using a
comma ','.

More about Enumerations


Enumerations internally use integral values to
represent the different named constants. The
underlying type of an enumeration can be any
integral type (e.g. int, byte, etc). The default
type is int
when we declare an enumeration, its items are
assigned successive integer values starting
from zero.

Example program
using System;
public enum Volume
{
Low,Medium,High
}
class EnumSwitch
{
static void Main()
{
Volume myVolume = Volume.Medium;
switch (myVolume)
{
case Volume.Low:

Console.WriteLine("The volume has been turned Down.");


break;
case Volume.Medium:
Console.WriteLine("The volume is in the middle.");
break;
case Volume.High:
Console.WriteLine("The volume has been turned up.");
break;
}
Console.ReadLine();
}
}

Garbage Collection
.Net provides its own Garbage Collector to manage the
memory
The Garbage Collector is a program that runs in the
background as a low-priority thread and keeps track of the
un-referenced objects (objects that areno longer referenced
by any reference) by marking them as 'dirty objects'.
The Garbage collector is invoked by the .Net Runtime at
regular intervals and removes the dirty objects from
memory
The garbage collector,calls the Finalize() method or
destructor of the object, so as to allow an object to free
itsresources

Nested Classes in C#
We can also define nested classes in C#.
Nested classes (also called inner classes) are defined
inside another class
the default access modifierfor nested classes is
private, which means that the nested class can not be
referenced and accessed outside the containing class
Another important point about nested classes is that
they can access all the (private, internal, protected,
public)static members of the enclosing class

Nested class program


using System;
class outer
{
public void add()
{
int a = 10, b = 20;
int c = a + b;
Console.WriteLine("Addition is " + c);
inner ins=new inner();
ins.sub();
}
class inner
{

public void sub()


{
int a=20;
int b=10;
int c = a - b;
Console.WriteLine("the subtraction is :" + c);
}
}
}
class nested
{
public static void Main()
{
outer ou = new outer();
ou.add();
}
}

Abstract Classes
Abstract classes can be simply defined as incomplete
classes
Abstract classes contain one or more incomplete
methods called abstract methods
The abstract class only provides the signature or
declaration of the abstract methods and leaves the
implementation of these methods to derived or subclasses
Abstract methods and abstract classes are marked
with the abstract keyword

They must be sub-classed in order to use


theirfunctionality. This is the reason why an
abstract class can't be sealed
A class inheriting an abstract class must
implement all the abstract methods in the
abstract class, or it too must be declared as an
abstract class
A class inheriting an abstract class and
implementing all its abstract methods is called
the concrete class of the abstract class.

Program for abstract class


using System;
abstract class MyAbs
{
public void NonAbMethod()
{
Console.WriteLine("Non-Abstract Method");
}
public abstract void AbMethod();
}
class MyClass : MyAbs
{
public override void AbMethod()
{
Console.WriteLine("Abstarct method");
}
}

class MyClient
{
public static void Main()
{
MyClass obj = new MyClass();
obj.NonAbMethod();
obj.AbMethod();
}
}

Interfaces
Interfaces are a special kind of type in C#, used to
define the specifications (in terms of method
signatures) that should be followed by its sub-types
An interface is declared using the interface keyword
interface can contain a signature of the methods,
properties and indexers
An interface is a type whose members are all public
and abstract by default.
An interface is implemented by a class. A class
implementing the interface must provide the body for
all themembers of the interface

To implement an interface, a class uses the


same syntax that is used for inheritance
A colon : is used to show that a class is
implementing a particular interface.
A class can implement more than one interface
contrary to class-inheritance where you can
inherit only one class. An interface itself can
inherit other interfaces

using System;
interface ISavable
{
void Read();
void Write();
}
public class TextFile : ISavable
{
public void Read()
{
Console.WriteLine("TextFile.Read()");
}
public void Write()
{
Console.WriteLine("TextFile.Write()");
}
}
public class demo : TextFile
{
public void addi()
{
int c=3+4;
Console.WriteLine("Addition is :"+c);
}

public void sub()


{
int a=4,b=3;
int c=a-b;
Console.WriteLine("Subtraction is :"+c);
}
}
public class Test
{
static void Main()
{
demo d=new demo();
d.Read();
d.Write();
d.addi();
d.sub();
}
}

S-ar putea să vă placă și