Interview Question

What is an extension method?

Extension methods add call syntax, not actual members or private access.

💡 Concept ✅ Quick Revision 🔷 C#

Answer

An extension method is a static method that can be called with instance-method syntax on its receiver type. • Traditional extension methods use this on the first parameter in a non-nested static class. • They do not modify the extended type or gain access to private members. • A real instance member takes precedence over an extension method with the same applicable signature.

💡 C# Example

public static class StringExtensions { public static bool HasText(this string? value) => !string.IsNullOrWhiteSpace(value); }

⚡ Quick Revision

Extension methods add call syntax, not actual members or private access.