Maha

Maha

  • NA
  • 600
  • 70.9k

Extension Method modified

Dec 18 2014 8:29 AM
http://www.dotnetperls.com/extension

Program in the above website is modified to work with the Reference Parameter. What changes have to be made to get the Dot net perls output in the Main() method. Problem is highlighted.

using System;

//class ExtensionMethods
//{
// public static string UppercaseFirstLetter(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";
UppercaseFirstLetter(ref value); // Called like an instance method.
Console.WriteLine(value);//Dot net perls

Console.ReadKey();
}
public static void UppercaseFirstLetter(ref 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]);

string s = new string(array);

Console.WriteLine(s);

//return new string(array);
}
//return value;
}
}


Answers (2)