Practice mode C#

Which requirement applies to declaring an extension method in C#?

Correct answer A. It must be a static method in a non-generic static class, with this before the first parameter

Explanation

An extension method is a static method in a top-level, non-generic static class, whose first parameter is prefixed with this, as in public static int WordCount(this string s). It can then be called as if it were an instance method: text.WordCount(). It cannot access private members of the extended type. LINQ operators such as Where and Select are extension methods on IEnumerable.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.