C# dynamic typing

Can C# script use the dynamic typing like in Javascript? Does C# contain similar functionality like the Javascript?

Javascript

var objectType = typeof(object); if (objectType == GUITexture) { //Do Something }

so.. what will be the C# code for the above Javascript?

Actually that's quite simple, but it is hard to find the correct documentation pages on that:

if(object is GUITexture){
    // Do Something
}

This also works with derived classes (up and down). You might also want to check the docs for GetType() and IsSubclassOf(), if you are working with derived classes. But the "is" keyword simplifies things. The actual dynamic cast would be:

GUITexture myTexture=object as GUITexture;

Avoid static casts ( myTexture=(GUITexture)obj ), as they will throw an exception and not compiler errors for derived classes.

EDIT: So, no there is no "real" dynamic typing / duck typing in (Unity-)C#. But with "is" and "as" you are still quite flexible, and the programmer himself can make sure that what he is trying to do is actually allowed (such as, casting from A to B).

Perhaps you can view this link? There is a 'dynamic' type in C#, and it sounds like that's what you want.

Since the dynamic keyword doesn’t work, the closest you could do is make interfaces.

http://lmgtfy.com/?q=c%23+interface+tutorials

As for the C# equivalent of:

var objectType = typeof(object); if (objectType == GUITexture) { //Do Something }

It would be:

var objectType = object.GetType(); if (objectType == typeof(GUITexture)) {  }