What "this is ClassName" statement mean in C#?

Viewed 118

I'm reading a C# project to implement a similar simple version of it in Python.

  • I'm not a C# programmer

Somewhere in codes there is a this and is statement together that I can't figure out what they are doing together:

public abstract class PredictionTrial
{

...

   public bool DoneSuccessfully
   {
      get
      {
          return (this is PredictionTrialSuccess);
      }
   }

...

}
  • PredictionTrialSuccess is a subclass of this class.

My question is that what this is is doing here with the combination of get?

2 Answers

If the dynamic type (the conrete type of your object at runtime) is PredictionTrialSuccess (or a subclass of it) then the property DoneSuccessfully will return true

It means that the class is assignable from PredictionTrialSuccess type.

Take a look at is keyword in C#.

A similar construction in python would be:

class A(object):
    def DoneSuccessfully(self):
        return isinstance(self, PredictionTrialSuccess)

class PredictionTrialSuccess(A):
    pass
Related