question

ReneeGA-8238 avatar image
0 Votes"
ReneeGA-8238 asked ReneeGA-8238 commented

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

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.

dotnet-csharpdotnet-runtime
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
1 Vote"
Viorel-1 answered ReneeGA-8238 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This looks great!

0 Votes 0 ·