http://www.dotnetperls.com/extension
This program is given in the above website.
In this program directive using Extensions; is not used. Please tell what is the reason for that?
In my view return type and variable in question are same type that is why directive is not used. I wish to know whether my understanding is correct. Problem is highlighted.
using System;
public static class ExtensionMethods
{
public static string UppercaseFirstLetter(this string value)
{
//
// Uppercase the first letter in the string this extension is called on.
//
if (value.Length > 0)
{
char[] array = value.ToCharArray();
array[0] = char.ToUpper(array[0]);
return new string(array);
}
return value;
}
}
class Program
{
static void Main()
{
//
// Use the string extension method on this value.
//
string value = "dot net perls";
value = value.UppercaseFirstLetter(); // Called like an instance method.
Console.WriteLine(value);//Dot net perls
Console.ReadKey();
}
}
Loading

VulpesPosted Dec 17, 2014, 6:46 AM
However, if you'd placed the static class in a namespace called Extensions, then you would have needed the using directive to use the extension method from the global namespace:
MahaPosted Dec 17, 2014, 10:00 AM
VulpesPosted Dec 17, 2014, 9:24 AM
MahaPosted Dec 17, 2014, 9:19 AM
if (value.Length > 0)
{
char[] array = value.ToCharArray();
array[0] = char.ToUpper(array[0]);
return new string(array);
}
return value;
MahaPosted Dec 17, 2014, 7:02 AM
Michal HabalcikPosted Dec 17, 2014, 6:44 AM
http://msdn.microsoft.com/en-us/library/bb383977.aspx