Taking into account the application code below, having 3 overloaded extension methods doing the same as well as having the same input and output:
- What is the benefit (or difference) in using and in parameters of calling and defining a method?
- Why to use them (if the same method 3 without them will be called and do the same)?
- And or how can they (such information) be manually/explicitly used inside a method?
Code:
namespace LINQ_CreatingYourFirstExtensionMethod
{
internal class Program
{
private static void Main(string[] args)
{
string[] aBunchOfWords = {"One", "Two", "Hello", "World", "Four", "Five"};
var iEnuResult = from s in aBunchOfWords
where s.Length == 5
select s;
iEnuResult.Write();
iEnuResult.Write<string>();
iEnuResult.Write<object>();//same effect as above line
Console.ReadKey();
}
}
}
The overloaded extension methods Write():
namespace LINQ_CreatingYourFirstExtensionMethod
{
public static class Utils
{
public static void Write(this IEnumerable<object> source) //****1
//public static void Write(this IEnumerable<string> source)//same effects as above line
{
foreach (var item in source)
{
Console.WriteLine(item);
}
}
public static void Write<T>(this IEnumerable<T> source)//****2
{
foreach (var item in source)
{
Console.WriteLine(item);
}
}
public static void Write(this IEnumerable source)//******3
{
foreach (var item in source)
{
Console.WriteLine(item);
}
}
}
}
Update:
Can you give me any simplest illustration why would I need
- call a method using
<Type>
- define a method using
<Type>
if I can do all the same without it?
Update2:
I did not write it at once since I thought it was obvious from context of question.
In current code:
- the
iEnuResult.Write();
call
enters the 1st method:
public static void Write(this IEnumerable source) //**1 - iEnuResult.Write(); (or iEnuResult.Write();) call
enters the 2nd meth: public static void Write1(this IEnumerable source)//**2
If to comment out
public static void Write1<T>(this IEnumerable<T> source)//*****2
then
iEnuResult.Write1<object>();
cannot be made (only iEnuResult.Write();
)
And if to comment both 1st and 2nd methods, the 3d one is called by
iEnuResult.Write();
So, it is possible to call "the same" method both with and without a <Type>
in the call (invocation) as well as to have a method definition with and without a <Type>
.
There are multiple combinations (I'd say dozens) to do seemingly the same things and I do not see much rationale for this.
So, I'd like to understand what is the purpose of such differences existence, if any