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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:36:52+00:00 2026-06-05T06:36:52+00:00

I have the following extension methods to help me check and instantiate objects if

  • 0

I have the following extension methods to help me check and instantiate objects if they are null. The top two work just fine but they are not very useful.

    public static bool IsNull<T>(this T t)
    {
        return ReferenceEquals(t, null);
    }

    public static T NewIfNull<T>(this T t, Func<T> createNew)
    {
        if (t.IsNull<T>())
        {
            return createNew();
        }

        return t;
    }

    public static void Ensure<T>(this T t, Func<T> createNew)
    {
        t = t.NewIfNull<T>(createNew);
    }

Ultimately I would like to do something like

IList<string> foo;
...
foo.Ensure<IList<string>>(() => new List<string>());

The Ensure method however does not achieve the desired effect, which is setting foo to a instance of List<string> if it is null and basically set it to itself otherwise.

if you know now I can tweak the Ensure method to achieve this I would appreciate the help.

Thanks, Tom

  • 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-05T06:36:54+00:00Added an answer on June 5, 2026 at 6:36 am

    You need to distinguish between objects and variables. An object can never be null – the value of a variable can be. You’re not trying to change something about an object (which would work) – you’re trying to change the value of the caller’s variable.

    However, arguments are passed by value by default, which means that your extension method changes the parameter (the variable declared within the method), but this has no effect on the caller’s variable. Normally you’d be able to change the parameter to ref to achieve pass-by-reference semantics, but extension methods can’t have a ref or out first parameter.

    As others have said, using the null-coalescing operator (??) is a better bet. Note that in this expression:

    foo = foo ?? new List<string>();
    

    the new list is not constructed unless foo is null. The right hand operand of ?? is not evaluated unless it needs to be.

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

Sidebar

Related Questions

I have following LINQ statement and I want to rewrite it using extension methods.
I have a prototype model where I need to include the following extension methods
Update: This does work, I was being stupid :( i have the following extension
I have written a following code to get just the file name without extension
I am experimenting with fluent extension methods. I have the following simple extension method
Say you have the following in a schema: <xsd:complexType name=shape/> <xsd:complexType name=circle> <xsd:complexContent> <xsd:extension
I am looking into strong typed Windows Forms databinding using extension methods. I have
I have the following extension method, and would like to make it more generic
I have the following extension method public static class ListExtensions { public static IEnumerable<T>
I have created the following extension of the observable list to mimic a readonly

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.