Video of the week

This is a must-watch video about one of us trying to reach the stars :-)

Well done #HRejterzy

[C#] What a complex declaration we've got here (..)

I've been familiar with C# for a long time and yet there are moments when I consider this language quite difficult to read. I want to create an extension method to be able to count a specific character in a string object. I could do it using simple foreach loop but no - let's try different approach. I wanna use the Count method and provide simple lambda expression as the parameter. When some people look at some complex declarations of various methods in C#, they usually say "What is that?!" - look at the following declaration of the Count method:

predicate

  1. int IEnumerable<char>.Count<char>(Func<char, bool> predicate)

 Let's try to decrypt the declaration which is not that easy to understand at first glance.

int - method returns int - number of a specific character in a string object, this is easy

IEnumerable<char>. - doesn't it look like: (IEnumerable<char>).method()? - so we have a simple OBJECT in brackets,
and the dot says that we're about to call one of its methods. No matter what, the object in brackets must implement the IEnumerable interface.

.Count<char> - our method which needs a simple character as the input parameter

(Func<char, bool> predicate) - Func is a delegate which needs a simple character as the input parameter
to provide a bool value as the result - so we need a piece of code here. It can be a method or a simple lambda expression.

The lambda expression is called for each character in the Array. Each character becomes the input paramater and each time either false (_c != c) or true (_c == c) value is return.