Beruflich Dokumente
Kultur Dokumente
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.
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).
Example program
using System;
public enum Volume
{
Low,Medium,High
}
class EnumSwitch
{
static void Main()
{
Volume myVolume = Volume.Medium;
switch (myVolume)
{
case Volume.Low:
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
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
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
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);
}