Generics - Basic
Generics
Definition
Generics are the most powerful feature of C# 2.0. Generics allow you to define type-safe data structures, without committing to actual data types. This results in a significant performance boost and higher quality code, because you get to reuse data processing algorithms without duplicating type-specific code. [MSDN]
Code
Compare.cs
public class Compare<DONTKNOWYET>
{
public bool CompareData(DONTKNOWYET d1, DONTKNOWYET d2 )
{
if(d1.Equals(d2))
{
return true;
}
return false;
}
}
{
public bool CompareData(DONTKNOWYET d1, DONTKNOWYET d2 )
{
if(d1.Equals(d2))
{
return true;
}
return false;
}
}
Program.cs
class Program
{
static void Main(string[] args)
{
Compare<int> intCompare = new Compare<int>();
Compare<string> stringCompare = new Compare<string>();
// Compare equal numbers
Console.WriteLine("3=3: ? ");
if (intCompare.CompareData(3, 3))
{
Console.WriteLine("Entered data is Equal!");
}
else
{
Console.WriteLine("Entered data is not Equal!");
}
// Compare unequal numbers
Console.WriteLine("3=10: ? ");
if (intCompare.CompareData(3, 10))
{
Console.WriteLine("Entered data is Equal!");
}
else
{
Console.WriteLine("Entered data is not Equal!");
}
// Compare equal string
Console.WriteLine("BYTERODEO=BYTERODEO: ? ");
if (stringCompare.CompareData("BYTERODEO", "BYTERODEO"))
{
Console.WriteLine("Entered data is Equal!");
}
else
{
Console.WriteLine("Entered data is not Equal!");
}
// Compare unequal string
Console.WriteLine("RODEOBYTE=BYTERODEO: ? ");
if (stringCompare.CompareData("RODEOBYTE", "BYTERODEO"))
{
Console.WriteLine("Entered data is Equal!");
}
else
{
Console.WriteLine("Entered data is not Equal!");
}
Console.Read();
}
}
{
static void Main(string[] args)
{
Compare<int> intCompare = new Compare<int>();
Compare<string> stringCompare = new Compare<string>();
// Compare equal numbers
Console.WriteLine("3=3: ? ");
if (intCompare.CompareData(3, 3))
{
Console.WriteLine("Entered data is Equal!");
}
else
{
Console.WriteLine("Entered data is not Equal!");
}
// Compare unequal numbers
Console.WriteLine("3=10: ? ");
if (intCompare.CompareData(3, 10))
{
Console.WriteLine("Entered data is Equal!");
}
else
{
Console.WriteLine("Entered data is not Equal!");
}
// Compare equal string
Console.WriteLine("BYTERODEO=BYTERODEO: ? ");
if (stringCompare.CompareData("BYTERODEO", "BYTERODEO"))
{
Console.WriteLine("Entered data is Equal!");
}
else
{
Console.WriteLine("Entered data is not Equal!");
}
// Compare unequal string
Console.WriteLine("RODEOBYTE=BYTERODEO: ? ");
if (stringCompare.CompareData("RODEOBYTE", "BYTERODEO"))
{
Console.WriteLine("Entered data is Equal!");
}
else
{
Console.WriteLine("Entered data is not Equal!");
}
Console.Read();
}
}
Output
Advantages
- Generics are Type Safe and eliminates overhead of multiple implementations.
- Generics does not require boxing and unboxing.
- Generics are checked on compile time so your code will be always correct at run time.
- Generics provide type constraints which helps you control the types that can be given as input to your class\method.
Disadvantages
- Code becomes complex for beginners.
- Generic types can be derived from most base classes, such as MarshalByRefObject (and constraints can be used to require that generic type parameters derive from base classes like MarshalByRefObject). However, the .NET Framework does not support context-bound generic types. A generic type can be derived from ContextBoundObject, but trying to create an instance of that type causes a TypeLoadException.
Comments
Post a Comment