Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6133905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:17:21+00:00 2026-05-23T17:17:21+00:00

I was looking at the mvc-mini-profiler designed by the Stack Overflow team on Google

  • 0

I was looking at the mvc-mini-profiler designed by the Stack Overflow team on Google Code and one thing on the getting started page struck me as particularly strange:

var profiler = MiniProfiler.Current; // it's ok if this is null

using (profiler.Step("Set page title"))
{
    ViewBag.Title = "Home Page";
}

How can it be “ok” if profiler is null? It seems to me that calling Step would throw a NullReferenceException. In all my years of programming C# I’ve never known calling a method on a null reference in any context to be “ok”. Is this a special case in the context of a using clause?

I can understand this being OK (didn’t know it was, but apparently it is?):

using (null)
{
    ...
}

but calling a method on a null reference seems like it should throw an exception regardless of whether it’s in a using clause. Can someone please explain how such a construct is translated behind the scenes, so I can understand why it is OK to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:17:22+00:00Added an answer on May 23, 2026 at 5:17 pm

    It’s absolutely not OK if profiler is null unless profiler.Step is actually an extension method. The using statement doesn’t affect that.

    As it turns out, the extension method part is exactly what’s going on. Lines 584-587 of MiniProfiler.cs:

    public static IDisposable Step(this MiniProfiler profiler, string name,
                                   ProfileLevel level = ProfileLevel.Info)
    {
        return profiler == null ? null : profiler.StepImpl(name, level);
    }
    

    That’s how it’s okay for profiler.Step to be called when profiler is null. It’s not an instance method – the call translates to:

    MiniProfilerExtensions.Step(profiler, ...);
    

    It’s fine for profiler.Step to return null, as per the second part of your question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just hooked up the mvc-mini-profiler (thanks SO!) on my site and was looking
Basically the questions in the title. I'm looking at the MVC 2 source code:
im looking at the MVC pattern, and I can see in one example on
I have just started looking into .net MVC and I really like it. There
I have incorporated the mini-profiler in mvc app but I have a question. When
I was looking over some ASP.NET MVC 1 code (C#) in search of the
I'm looking to create an MVC web page that displays the 12 months of
I'm looking into ASP.NET MVC, and whether or not to make the switch. One
I'm trying to use the mini-profiler with old-style EF code - database-first. So far:
Looking in the ASP.NET MVC 2 source code there are several files in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.