Search code examples
c#extension-methods

How come I can call IsNullOrWhiteSpace without specifying string class


During my research I found something interesting about IsNullOrWhiteSpace.

This is an extension method but I used to use it as string.IsNullOrWhiteSpace.

Somewhere I found I can call it without specifying string class:

public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var req = filterContext.HttpContext.Request;
        var auth = req.Headers["Authorization"];
        if (!IsNullOrWhiteSpace(auth))
        {
...

How it's possible and why it works?


Solution

  • I suspect you'll find this at the top of the file:

    using static System.String;
    

    The feature of using static directives was introduced in C# 6, and allows you to use any static member of the specified class without qualification.