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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:41:54+00:00 2026-06-09T15:41:54+00:00

Question I’d like to have a class that is able to handle a null

  • 0

Question

I’d like to have a class that is able to handle a null reference of itself. How can I do this? Extension methods are the only way I can think to do this but thought I’d ask in case there was some nifty thing I didn’t know about C#.

Example

I have a class called User with a property called IsAuthorized.

When User is properly instantiated IsAuthorized has an implementation. However, when my User reference contains null I’d like a call to IsAuthorized to return false instead of exploding.


Solution

Lot’s of good answers. I ended up using three of them to solve my problem.

  1. I used the Null Object design pattern suggested by Zaid Masud.
  2. I combined that with Belmiris’ suggestion of using struct so I couldn’t have a null reference
  3. And got a great explanation for why C# works this way and how I could really solve it from Jon Hanna

Unfortunately I can only pick one of these as my accepted answer so if you are visiting this page you should take the time to up vote all three of these and any of the other excellent answers that were given.

  • 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-09T15:41:55+00:00Added an answer on June 9, 2026 at 3:41 pm

    How about a proper Object Oriented solution? This is exactly what the Null Object design pattern is for.

    You could extract an IUser interface, have your User object implement this interface, and then create a NullUser object (that also implements IUser) and always returns false on the IsAuthorized property.

    Now, modify the consuming code to depend on IUser rather than User. The client code will no longer need a null check.

    Code sample below:

    public interface IUser
    {
        // ... other required User method/property signatures
    
        bool IsAuthorized { get; }
    }
    
    public class User : IUser
    {
        // other method/property implementations
    
        public bool IsAuthorized
        {
            get { // implementation logic here }
        }
    }
    
    public class NullUser : IUser
    {
        public bool IsAuthorized
        {
            get { return false; }
        }
    }
    

    Now, your code will return an IUser rather than a User and client code will only depend on IUser:

    public IUser GetUser()
    {
        if (condition)
        {
            return new NullUser(); // never return null anymore, replace with NullUser instead
        }
        return new User(...);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question: Can I override default functions in Javascript? Background: After figuring out that I
Question Is it possible to create a second Preference Dialog that can take some
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Question: Have I got this example backwards? Is the reason to return Interface so
Question : Can anyone tell me why my unit test is failing with this
Question Can I build a image database/library that has an e-commerce style checkout system
Question Please note that the solution to this is directly below using Eugen's view
Question How can I get this sequence to fire when a user takes action
question: Can i perform a flush like function to a richtextbox? reason: I am
Question Is it possible to stop a Message Driven Bean (programmatically), so that it

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.