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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:51:25+00:00 2026-05-25T19:51:25+00:00

I have what I presume must be a common problem. Lets say I have

  • 0

I have what I presume must be a common problem. Lets say I have a typical Person Class and I have a Car Class, which has a field/property owner of type Person:

public class Car
{
    Person owner;        
}

But sometimes I want the car to have no owner. What’s the recommended way of dealing with this problem. A Nullable type doesn’t work as reference types are already nullable.(Edit: clarification, I meant you cant use Person? owner;)

I could add in a bool field/property: hasOwner, but that seems rather verbose, so I thought of creating a static member none of type Person in the Person class like so:

namespace ConsoleApplication2
{
public class Person
{
    public static Person none;
    public int age;

    static Person()
    {
        none = new Person();
    }
}

public class Car
{
    public Person owner;
}

class Program
{
    static void Main(string[] args)
    {
        Car dumped = new Car();
        dumped.owner = Person.none;
        Console.ReadLine();
    }
}
}

This compiles and runs, although I haven’t used it in a real application. Then I thought I could make a generic class with a none member as so:

namespace ConsoleApplication2
{
public class NoneMember<T> where T : new()
{
    public static T none;

    static NoneMember ()
    {
        none = new T();
    }
}

public class Person: NoneMember<Person>
{        
}

public class Car
{
    public Person owner;
}

class Program
{
    static void Main(string[] args)
    {
        Car dumped = new Car();
        dumped.owner = Person.none;
        if (dumped.owner == Person.none)
            Console.Write("This car has no owner");
        Console.ReadLine();
    }
}
}

That compiles and runs, although there would be a problem if you wanted to inherit form the person class, with the generic and non generic versons. The thoughts of more experienced c# programmers would be appreciated.

Edit: The problem with using null to represent none is that you can’t pass null as a method / constructor parameter. null means value not set, which is different from value is none.

Edit2: I thought I was getting exceptions thrown when I passed null parameters. Not sure what was going on. I don’t know if I can reproduce the errors now.

  • 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-25T19:51:25+00:00Added an answer on May 25, 2026 at 7:51 pm

    I usually leave it as it is, if Car’s owner is null, that means just that – it does not have an owner. There is no need to complicate, if there is value – it has owner, otherwise it does not have (known to system) owner.
    Exception from this policy is the case when you need to have journaling database, but in that case you usually can have versioned cars and owners and use the same principle.

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

Sidebar

Related Questions

Presume you have a class which passes all its current unit tests. If you
I have a curl class, called Curl. Let's presume i have this code: $url
I have a record returned from MySQL that has a datetime field. What I
I have successfully created a asp mvc app which basically has interface, service and
I have a an application that has 2 parts. A service which creates content.
Let's presume that I have string like '=&?/;#+%' to be a part of my
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Let us presume we have k sequences of fixed length p . Each sequence
Im using the above technologies and have ran into what I presume is a
For my university class we are developing a multi-threaded Blackberry application which allows us

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.