Reputation: 5222
I was going through some of the queries in LINQ and wanted to understand its implementation, so i thought of debugging the same but when i tried to do it Visual Studio is not entering into the implementation of the interface don't know why is it. I am using Visual Studio Community 2015. Here is my code
class Client
{
static void Main(string[] args)
{
string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
var result = words.OrderBy(a => a, new CaseInsensitiveComparer());
Console.Read();
}
}
public class CaseInsensitiveComparer : IComparer<string>
{
public int Compare(string x, string y)
{
Console.WriteLine("x is " + x + " & y is " + y+" the value is "+ string.Compare(x, y, StringComparison.OrdinalIgnoreCase));
return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
}
}
and the saddest part is i am also not able to print anything in my console window
Console.WriteLine("x is " + x + " & y is " + y+" the value is "+ string.Compare(x, y, StringComparison.OrdinalIgnoreCase));
I understand there are many duplicate questions regarding this but i tried everything and none of them worked for me.
UPDATE 1
I have placed my debugger inside the implementation of the IComparer
Upvotes: 2
Views: 553
Reputation: 82934
Your .OrderBy()
call will only be evaluated when you use its result (as is the case with a lot of the linq methods). As you aren't using the result, the code is not actually running.
Put a .ToList()
on the end and it will run:
var result = words.OrderBy(a => a, new CaseInsensitiveComparer()).ToList();
You probably won't be able to step into the .OrderBy()
call, but you will be able to put a breakpoint inside your comparer implementation.
Upvotes: 5
Reputation: 5735
OrderBy returns an IEnumrable
you to instate your Linq query , add ToList()
after the OrderBy
clause
var result = words.OrderBy(a => a, new CaseInsensitiveComparer()).ToList();
Upvotes: 0