If this question is too simple I apologize in advance.
Why does the method NthIndexOf found in the link below require a static class and also static class member?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because it’s an EXTENSION METHOD (EM).
For example:
String is a .Net type. Prior to EM, without deriving, you cannot add your own method to string type. Even if you did so by deriving, that method would be counted as method of derived type and not of string type.
But now with EM’s you can do so without deriving from it.
Requirements of EM (in C#)
thiskeyword in front of it. Without it, your method would not be an EM and would be a static method only.In your earlier scenaro your EM
NthIndexOfis defined in static classStringExtenderNow since first parameter contains this in front of string so you can call it as
If it was not EM and a plain static method like this
then it had to be called like