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 9228591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:21:44+00:00 2026-06-18T05:21:44+00:00

So, I really enjoy using extension methods.. maybe a bit too much. So, I’m

  • 0

So, I really enjoy using extension methods.. maybe a bit too much. So, I’m going to ask about my latest enjoyment to ensure that I’m not going too far.

Scenario is that we have a Guid? variable that gets passed in. If the variable is null or Guid.Empty, then we want to use a different Guid. So, I wrote an extension method to make it read like English:

    internal static Guid OrIfEmpty(this Guid? guid, Guid other)
    {
        if (!guid.HasValue || guid.Value == Guid.Empty)
        {
            return other;
        }
        return guid.Value;
    }

This automatically implies that a “null this” will not throw an exception. For instance, this will work:

((Guid?)null).OrIfEmpty(other);

This is not possible without using extension methods, and in my opinion can be quite misleading. However, it’s just so concise and clean! So, what do you think? Is this an acceptable thing to do or could it be too confusing to other programmers?

Also, I’m sure there will be other scenarios where I do things like this and checking this for null, but this is the best example I have right now.

Note: I’m not really asking about this Guid? business in particular. I’m asking more about the overall pattern implemented (having an extension method where the this can be null)

  • 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-06-18T05:21:45+00:00Added an answer on June 18, 2026 at 5:21 am

    This is not possible without using extension methods

    Sure it is, just make is a regular static method call (which is all extension methods are):

    in my opinion can be quite misleading

    I agree, since it looks like you’re calling an instance method on a null instance. It could be worse:

    string s = null;
    string n = s.OrIfEmpty("empty");
    

    At first glance this looks like an obvious NullReferenceException waiting to happen, but it compiles and works as designed.

    Since your question is really just soliciting opinions, there’s not one right answer, but I certainly would be cautious and document the extension method to indicate that the this parameter could be null. Or (as @quezalcoatl implies) rename it to be more explicit that it supports null values:

    internal static Guid OrIfNullOrEmpty(this Guid? guid, Guid other)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've begun using ReSharper and really enjoy it. However I have a project which
I do some hobby work with PIC Microcontrollers and I really enjoy using them
In Python, I really enjoy how concise an implementation can be when using list
I have been using Prism for a while now and enjoy how much easier
I've started using Pivotal Tracker and really enjoy it. I've also started looking at
I really enjoy using upstart. I currently have upstart jobs to run different gunicorn
I use both ruby on rails and Java. I really enjoy using migrations when
I really enjoy to be able to do this in C# : IEnumerable GetThePizzas()
Really confused why the QR output using RcppArmadillo is different than QR output from
Just discovered ServiceStack last months and i really enjoy working with this great framework.

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.