Skip to main content

Introduction to Extension methods

අද අපි කතා කරන්න ලෑස්ති වෙන්නේ C# වල තියෙන අපිට ප්‍රයෝජනවත් වෙන තවත් feature එකක් ගැන. ඒ තමා මේ Extension Methods.

අපි පුරුදු විදිහටම සරල උදාහරණයක්‌ එක්කම මේක පටන් ගමු.

 using System;  
 namespace ExtensionTest  
 {  
   static class ExtensionMethods  
   {  
     public static int GetSquaredValue(this int num)  
     {  
       return num * num;  
     }  
   }  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       int i = 20;  
       int squaredValue = i.GetSquaredValue();  
       Console.WriteLine("The squared value of {0} is {1}.",i,squaredValue);  
     }     
   }  
 }  

දැන් බලමු මොකක්ද මෙතන ඇත්තටම සිද්ද වෙන්නේ කියල. ඕනේ නම් මේ code එක run කරලා බලන්නකො වැඩ කරනවා ද කියල.
  1. මුලින්ම අපි කරලා තියෙන්නේ static class එකක් declare කරනවා අපේ Extension method එක ලියාගන්න.
  2. ඊට පස්සේ අපි කරන්නේ අපේ Extension method එක ලියාගන්නවා මේ class එක ඇතුලේ. මෙතන අපි ගන්නේ ඉලක්කමක වර්ගය හොයාගන්න පුළුවන් method එකක් GetSquaredValue කියල නේ නම දාල තියෙන්නෙත්.
  3. දැන් අපි පොඩ්ඩක් බලමු මේ method එකේ signature දිහා. මේකේ තියන පලවෙනි parameter එකෙන් කියවෙන්නේ මේ method එක අදාළ වෙන්නේ මොන data type එකටද කියන එක. දැන් මේකේ හැටියට නම් මේ method එක අදාල වෙන්නේ int වලට තමා.  ඒ වගේම පලවෙනි parameter එකට ඉස්සරහින් this කියන keyword එක අනිවාර්යෙන්ම දාන්න ඕනෙ. (හැබැයි අපේ method එකේ තියෙන්නේ එක parameter එකයි උනාට, සාමාන්‍ය method එකක වගේ මේකටත් ඕනේ තරම් parameters තියෙන්න පුළුවන්).
  4. ඊට පස්සේ ඉතින් method එක ඇතුලේ අපිට වෙන්න ඕනෙ වැඩේ කරන්න ඕනෙ. දැන් මේකේ හැටියට නම් අපි ඉලක්කමේ වර්ගය හොයාගන්න පොඩි code එකක් ලියල තියෙනවා.
  5. දැන් බලමු මේක call කරගන්නේ කොහොමද කියල. අපි සාමාන්‍ය int variable එකක් declare කලාම ඒකේ methods list එකට මේ Extension method එකත් ඇවිල්ල තියෙනවා ඔයාලට පෙනෙයි.
  6. Calling Extension method
  7. ඒක call කලාම ඉතින් result එක හම්බ වෙනවා.  මෙතන වැදගත් දෙයක් තමා අපි පලවෙනි parameter එක pass කරන්න ඕනෙ වෙන්නේ නෑ. ඒ මොකද අපි මේක call කරන variable එකේ value එක පලවෙනි parameter එක විදිහට automatically pass වෙනවා.
ඉතින් අපි මේ වැඩේ ලොකු project එකක එහෙම කරනවා නම් මේ අපේ Extension method එක define කරලා තියෙන class එක අපිට පුළුවන් වෙනම dll එකක තියාගෙන, ඒ dll එකට reference දාගෙන පාවිච්චි කරන්න.

මොනවා හරි අවුල් තියෙනවා නම් ඉතින් අහන්න.

Comments

Popular posts from this blog

Adding Unique Constraints with Entity Framework Code First

Entity Framework Code First is a great way to define and maintain your database within your application it self. While it poses a nice set of complementing libraries like Data Annotations and Fluent Configurations which helps you specially in defining phase of your database, it would give you a headache if you try to define a unique constraint on a column. For example in the users table of your application you could probably have an int ID column which would serve as the primary key and you might need to make your Username column a unique one. Since you are using EF Code First you will soon find out there is no direct way to accomplish this requirement. Unfortunately fluent configurations doesn't have syntax like HasUnique(u => u.Username); I asked the same question in stackoverflow , but didn't get a convincing answer. Since there is no direct support from EF for this you could take one of following alternatives to achieve it. Approach 1 :  ...

Building Highly Scalable Web Applications with Windows Azure

Among many other benefits of moving or building your web applications in cloud, for me I think the most important benefit we get is scalability. When it comes to web applications there are two approaches for scalability. Scale out - This means we increase the number of running instances of the application with a load balancer which distributes the requests among those instances Scale up - This means we increase the physical resources on a single application instance, for example we can increase the RAM of the hosted machine In reality preferred way of web application scalability should be to scale out. Because there are obvious hardware limitations in scale up approach. Bearing that in mind if we look at what Windows Azure provides out of the box, for a certain level it supports scale up approach and it has a great amount of support for scale out approach. Scaling your application out Windows Azure supports both automatic and manual scale out of the ap...

HTML INPUT enhancement with Regular Expressions and Java Scripts

අද කථා කරන්න යන්නේ බොහොම සරල දෙයක් ගැන. සරල උනාට ඉතින් මට මේක කරගන්න ඕන වෙලා හොයන කොට නම් ඉතින් හොයාගන්න ටිකක් කරදර උනා, ඒ නිසාම තමා මම හිතුවේ මේක ගැන ලියන්න ඕනෙ කියල. අපි හැමෝම HTML forms validate කරන්න Java Script පාවිච්චි කරනවා නේ, එක අලුත් දෙයක් නෙමේ. ඒ වගේම තමා Regular Expressions කියන්නෙත් අපිට අලුත් දෙයක් නෙමේ. අපි බලමු මේ දෙකම පාවිච්චි කරලා HTML textbox එකකට අපිට ඕනෙ characters විතරක් enter කරන්න දෙන්නේ කොහොමද කියල. මෙන්න මේක තමා අපි පාවිච්චි කරන HTML page එක. <!DOCTYPE html> <html> <head> <title>JS and RegEx</title> </head> <body> <label for="txtUsername">Username</lable> <input type="text" id="txtUsername" placeholder="Enter Username"/> </body> </html> මේ තියෙන page එක html විදිහට save කරලා browser එකේ බැලුවම මෙන්න මේ වගේ තමා පෙන්නේ. දැන් හිතන්න අපිට මේ username එකට @,#,%,$ වගේ...