Maha

Maha

  • NA
  • 600
  • 66.9k

Extension Method and Directive

Dec 17 2014 6:32 AM
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();
}
}


Answers (6)