Extension methods in csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
string post = "Hi this is my first post";
var shortpoint = post.shorten(1).Captital(true);
Console.WriteLine(shortpoint);
Console.Read();
}
}
public static class ShortenString
{
public static string shorten(this string a , int length)
{
a.Take(length);
return a;
}
public static string Captital(this string a, bool type)
{
if(type)
return a.ToUpper();
else
{
return a;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
string post = "Hi this is my first post";
var shortpoint = post.shorten(1).Captital(true);
Console.WriteLine(shortpoint);
Console.Read();
}
}
public static class ShortenString
{
public static string shorten(this string a , int length)
{
a.Take(length);
return a;
}
public static string Captital(this string a, bool type)
{
if(type)
return a.ToUpper();
else
{
return a;
}
}
}
}
Notes: Extension methods are used when we are not given unable to change type of class by inheriting the class. here we need to create public static class and add the static methods we are planning to extend for that class.
here while extending a class you need to mention type of class you are extending as highlighted.
Extension methods in csharp
Reviewed by Share less to Learn More
on
3:33 PM
Rating:
knowledgeable programming from this. keep sharing more information for us. i want to keep take more developing from your blog. expecting more information.
ReplyDeletedot net training in chennai