ஜெனரிக்ஸ் சி ஷார்ப்-2.0
உடன் அறிமுகப்படுத்தப்பட்டது. இது கிளாஸ் மற்றும் மெத்தட்களை குறிப்பிட்ட டேட்டா டைப்
உடன் மட்டும் பிணைக்கப்படுவதை தடுக்க பயன் படுகின்றது.
எடுத்து காட்டாக
பின் வரும் கிளாஸை எடுத்துக்கொள்வோம்.
class Comparer
{
public static bool AreEqual(int Val1,
int Val2)
{
return Val1 == Val2;
}
}
மேலே Comparer
என்னும் கிளாஸ் உள்ளது. இதில் AreEqual என்னும் ஸ்டேடிக் மெத்தட் உள்ளது. இது இரு
int மதிப்புகளை பராமீட்டராக ஏற்கின்றது. இரண்டும் சமமாக உள்ளதா என ஒப்பிட்டு ஒரு பூலியன்
மதிப்பை திருப்பி அனுப்புகின்றது.
class Program
{
static void Main(string[] args)
{
bool Result=Comparer.AreEqual (5,5);
if(Result )
{
Console .WriteLine ("same");
}
else
{
Console .WriteLine ("different");
}
Console.ReadLine();
}
}
வெளியீடு:
Same
class Program
{
static void Main(string[] args)
{
bool Result=Comparer.AreEqual ("x","x");
if(Result )
{
Console .WriteLine ("same");
}
else
{
Console .WriteLine
("different");
}
Console.ReadLine();
}
}
மேலே உள்ள நிரலில் AreEqual மெத்தடிற்கு இரண்டு
String மதிப்புகளை அனுப்பியுள்ளோம். இது டைப் செய்யும் போதே படத்தில் உ:ள்ளவாறு பிழை
சுட்டப்படும். பார்க்க Figure 1
Figure 1
பிழை சுட்டப்பட்டதற்கு
காரணம் AreEqual மெத்தட் ஆனது இரு int மதிப்புகளை மட்டுமே ஏற்கும். அது எந்த டைப் மதிப்பையும்
ஏற்க செய்வதற்கு பின் வரும் முறையை பின்பற்றலாம்.
using System;
namespace Generics
{
class
Program
{
static void Main(string[] args)
{
bool Result=Comparer.AreEqual ("x","x");
if(Result )
{
Console .WriteLine
("same");
}
else
{
Console .WriteLine ("different");
}
Console.ReadLine();
}
}
class
Comparer
{
public static bool AreEqual(object Val1,object Val2)
{
return Val1 == Val2;
}
}
}
இப்போது
AreEqual மெத்தடானது இரண்டு object வகை மதிப்புகளை ஏற்கின்றது.Object என்ற இனத்திலிருந்து
தான் எல்லாவகையான டேட்டா டைப்புகளும் சி ஷார்ப்பில் டெரிவ் செய்யப்படுகின்றன. எனவே
object டைப்பானது எல்லாவகையான மதிப்புகளையும் ஏற்கும். எனினும் இதில் இரு சிக்கல்கள்
உள்ளன.
using System;
namespace Generics
{
class Program
{
static void
Main(string[] args)
{
bool
Result=Comparer.AreEqual (5,5);
if(Result )
{
Console
.WriteLine ("same");
}
else
{
Console
.WriteLine ("different");
}
Console.ReadLine();
}
}
class Comparer
{
public static bool
AreEqual(object Val1,object Val2)
{
return Val1 ==
Val2;
}
}
}
மேலே உள்ள நிரலில்
main மெத்தடில் AreEqual மெத்தட் அழைக்கப்படும்
பொழுது இரண்டு int இன மதிப்புகள் அனுப்பப்படுகின்றன. ஆனால் AreEqual மெத்தடானது அதை
object இனமாக மாற்றுகின்றது. பொதுவாக value இனமானது ஆப்ஜெக்ட் ஆக மாறுவது boxing எனப்படும்.
இங்கு பாக்ஸிங் நடை பெருவதால் performance degrade ஆகும்.மேலும் பின் வருமாறு இரண்டு
வெவ்வேறு வகையான டேட்டா வகை அழைக்கப்டும்
போது error ஏற்படும்.
bool
Result=Comparer.AreEqual (“x”,5);
இச்சிக்கல்களை
நீக்க generic டைப் பயன் தருகின்றது.முதலில் generic வகை பயன் படுத்தி எவ்வாறு நிரலை
திருத்தி அமைக்கலாம் என்று பார்ப்போம்.
முதலில்
AreEqual மெத்தட் ஆனது பின் வருமாறு மாற்றப்படுகின்து.
class Comparer
{
public static bool AreEqual<T>(T
Val1,T Val2)
{
return Val1.Equals(Val2);
}
}
இப்போது எந்த வகையான
டேட்டா வகை கொண்டும் இந்த மெத்தடை அழைக்கலாம்.
class Program
{
static void
Main(string[] args)
{
bool
Result=Comparer.AreEqual<int> (5,5);
if(Result )
{
Console
.WriteLine ("same");
}
else
{
Console
.WriteLine ("different");
}
Console.ReadLine();
}
}
மேலே உள்ள நிரலில்
இரண்டு ஸ்டிரிங் வகை மதிப்பை அனுப்புவதற்கு பின் வருமாரு அழைக்கலாம்.
bool Result=Comparer.AreEqual<String> (“x”,”x”);
அல்லது கிளாஸ்
டைப்பையே generic ஆக பின் வருமாறு மாற்றி அமைக்கலாம்.
using System;
namespace Generics
{
class
Program
{
static void Main(string[] args)
{
bool Result=Comparer<int>.AreEqual (5,5);
if(Result )
{
Console .WriteLine ("same");
}
else
{
Console .WriteLine ("different");
}
Console.ReadLine();
}
}
class
Comparer<T>
{
public static bool AreEqual(T
Val1,T Val2)
{
return
Val1.Equals(Val2);
}
}
}
பின் குறிப்பு:
இங்கு T என்று நாம் generic வகையை குறிப்பிடும்
எழுத்து ஒரு convention தான் அதற்கு பதிலாக நாம் வேறு எழுதுகளையும் பயன் படுத்தலாம்.
-முத்து
கார்த்திகேயன்,மதுரை
No comments:
Post a Comment