Any way to Handle 'this is null' inside class?

Renee GA 41 Reputation points
2021-06-21T06:16:19.71+00:00

Hi C# masters, I've got a newbie question:

We are a group of designers trying to do programming. I'm making APIs and classes for other people.

Problem is they are not good at doing null checking before calling methods. So I prefer to handle 'this is null' issues my side.

The goal is to handle null without throw any exception, and allowing the program to continue running.

Then I've looked at C# documents, and see that I can do something with the new nullable features.

e.g

 Foo? foo;
 ...
 foo?.Test();

    class Foo
    {
        public void Test()
        {
 Console.WriteLine("Do something.");
        }
    }

The code above totally works. The only issue is, when foo is null, the program just silently moved on - I do want to have the program to move on, but what I hoped is to also have some warning message to tell the user: "hey, your object is null, do some checking."

Preferably, I hope that I can do something inside the class Foo, so that when a variable of Foo type is not point to any object, but still try to call some of its method, it can still do something as a default behavior.

    class Foo
    {
        // Something like this
        void DefaultNullHandler()
        {
            if (this == null)
            {
                Console.WriteLine("Hey, your Foo object is null");
            }
        }
        // or even this, I can afford to add null checking to all of my method if this is possible
 public void Test()
 {
 if (this == null)
 {
 Console.WriteLine("Hey, your Foo object is null");
 }
 }
    }

I'm not sure if such things above is even possible, as when you call foo?.Test() or foo.Test() out side, the code is already doing the null checking before it even goes into the the 'Test' function, so (this == null) just won't work here.

So I came here and ask for help. Basically my goal is trying to figure a best way to handle null in side my APIs, and allow users to not need to worry about null checking before calling methods, while at the same time showing some warning to them if the object is null.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,216 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,118 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112K Reputation points
    2021-06-21T07:20:57.087+00:00

    Try something like this:

    public class Foo
    {
       internal void DoSomething( )
       {
          Console.WriteLine( "Do something." );
       }
    }
    
    public static class MyExtensions
    {
       public static void Test( this Foo f )
       {
          if( f == null )
          {
             Console.WriteLine( "Null" );
          }
          else
          {
             f.DoSomething( );
          }
       }
    }
    

    The usage is foo.Test(), without '?'.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful