First we create a normal integer stack class .
Example of integer stack class below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CAGenericExamples
{
//Stack of Integer
public class Stack
{
int[] data;
int top = -1;
public Stack(int size)
{
data = new int[size];
}
public void push(int value)
{
top++;
data[top] = value;
}
public int pop()
{
int value = data[top];
top--;
return value;
}
public int GetTopElement()
{
return data[top];
}
public void print()
{
for (int i = 0; i <= top; i++)
{
Console.WriteLine(data[i]);
}
}
}
class Program
{
static void Main(string[] args)
{
Stack s = new Stack(5);
s.push(5);
s.push(12);
s.print();
int n = s.pop();
Console.WriteLine(n);
s.print();
Console.ReadLine();
}
}
}
Example of integer stack class below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CAGenericExamples
{
//Stack of Integer
public class Stack
{
int[] data;
int top = -1;
public Stack(int size)
{
data = new int[size];
}
public void push(int value)
{
top++;
data[top] = value;
}
public int pop()
{
int value = data[top];
top--;
return value;
}
public int GetTopElement()
{
return data[top];
}
public void print()
{
for (int i = 0; i <= top; i++)
{
Console.WriteLine(data[i]);
}
}
}
class Program
{
static void Main(string[] args)
{
Stack s = new Stack(5);
s.push(5);
s.push(12);
s.print();
int n = s.pop();
Console.WriteLine(n);
s.print();
Console.ReadLine();
}
}
}
==============================================================
Next we create a normal object stack class .
Example of object stack class below: it is Non Generic Stack Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CAGenericExamples
{
class NonGenericStackClass
{
object[] data;
int top = -1;
public NonGenericStackClass(int size)
{
data = new object[size];
}
public void push(object value)
{
top++;
data[top] = value;
}
public object pop()
{
object value = data[top];
top--;
return value;
}
public object GetTopElement()
{
return data[top];
}
public void print()
{
for (int i = 0; i <= top; i++)
{
Console.WriteLine(data[i]);
}
}
}
class NonGenericProgram
{
static void Main(string[] args)
{
NonGenericStackClass s = new NonGenericStackClass(5);
s.push(5);
s.push("CSharp");
s.push(12);
s.push(new Stack(5));
s.print();
Console.WriteLine(s.pop());
s.print();
Console.ReadLine();
}
}
}
=========================================================
From Above Two Example we know the difference between a specific datatype class and an object class.
Now we Create a generic Class of same above stack class for clear understanding
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CAGenericExamples
{
class GenericClass<T>
{
T[] data;
int top = -1;
public GenericClass(int size)
{
data = new T[size];
}
public void push(T value)
{
top++;
data[top] = value;
}
public T pop()
{
T value = data[top];
top--;
return value;
}
public T GetTopElement()
{
return data[top];
}
public void print()
{
for (int i = 0; i <= top; i++)
{
Console.WriteLine(data[i]);
}
}
}
class GenericProgram
{
static void Main(string[] args)
{
GenericClass<int> s = new GenericClass<int>(5);
s.push(5);
//s.push("CSharp"); ERROR Comes here if un comment
s.push(12);
s.print();
Console.WriteLine(s.pop());
s.print();
GenericClass<string> ss = new GenericClass<string>(2);
ss.push("CSharp");
ss.push("Demo");
ss.print();
Console.ReadLine();
}
}
}
Example of object stack class below: it is Non Generic Stack Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CAGenericExamples
{
class NonGenericStackClass
{
object[] data;
int top = -1;
public NonGenericStackClass(int size)
{
data = new object[size];
}
public void push(object value)
{
top++;
data[top] = value;
}
public object pop()
{
object value = data[top];
top--;
return value;
}
public object GetTopElement()
{
return data[top];
}
public void print()
{
for (int i = 0; i <= top; i++)
{
Console.WriteLine(data[i]);
}
}
}
class NonGenericProgram
{
static void Main(string[] args)
{
NonGenericStackClass s = new NonGenericStackClass(5);
s.push(5);
s.push("CSharp");
s.push(12);
s.push(new Stack(5));
s.print();
Console.WriteLine(s.pop());
s.print();
Console.ReadLine();
}
}
}
=========================================================
From Above Two Example we know the difference between a specific datatype class and an object class.
Now we Create a generic Class of same above stack class for clear understanding
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CAGenericExamples
{
class GenericClass<T>
{
T[] data;
int top = -1;
public GenericClass(int size)
{
data = new T[size];
}
public void push(T value)
{
top++;
data[top] = value;
}
public T pop()
{
T value = data[top];
top--;
return value;
}
public T GetTopElement()
{
return data[top];
}
public void print()
{
for (int i = 0; i <= top; i++)
{
Console.WriteLine(data[i]);
}
}
}
class GenericProgram
{
static void Main(string[] args)
{
GenericClass<int> s = new GenericClass<int>(5);
s.push(5);
//s.push("CSharp"); ERROR Comes here if un comment
s.push(12);
s.print();
Console.WriteLine(s.pop());
s.print();
GenericClass<string> ss = new GenericClass<string>(2);
ss.push("CSharp");
ss.push("Demo");
ss.print();
Console.ReadLine();
}
}
}
=========================================================
From the Above generic Class as example ,, we create a generic Collection class example below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CAGenericExamples
{
class student
{
public int id;
public string name;
public student(int id, string name)
{
this.id = id;
this.name = name;
}
}
class GenericCollectionClass
{
static void Main(string[] args)
{
List<student> s = new List<student>();
s.Add(new student(1, "s1"));
s.Add(new student(2, "s2"));
s.Add(new student(3, "s3"));
s.Add(new student(4, "s4"));
foreach (student ss in s)
{
Console.WriteLine(ss.id +" "+ ss.name);
}
Console.WriteLine();
/// Other way of printing student details collection
IEnumerator<student> en = s.GetEnumerator();
while (en.MoveNext())
{
student st = en.Current;
Console.WriteLine(st.id +" "+st.name);
}
Console.ReadLine();
}
}
}
No comments:
Post a Comment