In C # should the keyword “always” be followed by a class?

In ActionScript, you can have a variable containing a reference to the type of a class, and then compare the class instance with the variable using is. Example:

var a:Foo = new Foo();
var type:Class = Foo;

if(a is type){  //this is true
  //do something
}

Can you do something like this in C #? Or should the keyword “always” be followed by a class?

+3
source share
4 answers

see also the Type.IsInstanceOfType method in the .NET platform if you want to test a type variable

+10
source

It can also be followed by an interface name.

+1
source

.NET 2010 beta 2: FrameWork 4, 3.5 ( "" FrameWork, "Client Profile" )

:

public class Foo
{
    public Foo() {}
}

:

Foo myFoo = new Foo();

Console.WriteLine(myFoo is Foo);

Console.WriteLine(typeof(Foo).IsInstanceOfType(myFoo));

Console.WriteLine "True" .

, "False to the Console Window":

Console.WriteLine(myFoo.GetType().IsInstanceOfType(typeof(Foo)));
+1
source

Data types are objects, so in C #

The following values ​​apply:
if (value is String) ...

if (value is ExampleClass) ...
0
source

Source: https://habr.com/ru/post/1728007/


All Articles