Ravendra's Blog

May 18, 2010

C# Extension Methods and Indexers

Filed under: c#, .Net, Extension methods, Indexer — Ravendra Mishra @ 4:57 pm

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ClassLibrary1

{


///
<summary>


/// The indexers in C# can be overloaded just likemember functions


/// There is no concept of static Indexer


/// A Base class indexer can be polymorphicalyoverridden in a Derived class.


/// But remember that the modifiers likevirtual, override etc are using at property level, not at accessorlevel.


/// Abstract Indexers


/// An indexer inside a class can be declared asabstract by using the keyword abstract.


/// Remember that an abstractindexer in a class carries no code at all. The get/set accessors


/// aresimply represented with a semicolon. In the derived class we mustimplement both set and get assessors.


/// If the abstract class contains only set accessor, we can implement only set in the derived class.


/// Indexers & Properties


/// 1. An index is identified by it’s signature. But a property is identified it’s name.


/// 2. An indexer is always an instance member, but a property can be static also.


/// 3. An indexer is accessed through an element access. But a property is through a member access.


///
</summary>


public
class
IndexerDemo

{


private
string[] data;

 


public IndexerDemo(int Size)

{

data = new
string[Size];

}

 


public
string
this[int index]

{


get

{


return data[index];

}


set

{

data[index] = value;

}

}

 


public
int
this[string str]

{


get

{


for (int i = 0; i < 5; i++)


if (data[i] == str)


return i;

 


throw
new
IndexOutOfRangeException(str + ” is not a key.”);

}

}

}

 


///
<summary>


/// Demo of extension method


/// Extension methods enable you to “add” methods to existing types (Class/Interface) without creating a new derived type,


/// recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method,


/// but they are called as if they were instance methods on the extended type.


/// The most common extension methods are the LINQ standard query operators that add query functionality


/// to the existing System.Collections.IEnumerable and System.Collections.Generic.IEnumerable<T> types.


/// 1. Extension methods need to be deinfed at top level (Non nested) static class


/// 2. First parameter of Extension method should be of the type on which this method is going to be implemented.


/// 3. extension methods are to extend a class or interface, but not to override them.


/// If same signature method is present in type definition (instance method) and also as extension method


/// then instance method will be called.


/// 4. In general, we recommend that you implement extension methods sparingly and only when you have to. Whenever possible,


/// client code that must extend an existing type should do so by creating a new type derived from the existing type.

 


///
</summary>


public
static
class
MyExtensions

{


public
static
int WordCount(this
String str)

{


return str.Split(new
char[] { ‘ ‘, ‘.’, ‘?’ }, StringSplitOptions.RemoveEmptyEntries).Length;

}

 


public
static
int FindIndex(this
IndexerDemo obj, String str )

{


for (int i = 0; i < 5; i++)


if (obj[i] == str)


return i;

 


throw
new
IndexOutOfRangeException(str + ” is not a key.”);

}

}

 


public
class
test

{


public
static
void Main()

{


String str = “Hello”;

str.WordCount();

 


IndexerDemo obj = new
IndexerDemo(5);

 


foreach (string s in obj)

{

 

}

 

obj[4]=“Hello”;

 

System.Console.WriteLine(obj[4]);

System.Console.WriteLine(obj[“Hello”]);

System.Console.WriteLine(obj.FindIndex(“Hello1”));

 

}

}

}

Advertisements

Blog at WordPress.com.