Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

To check whether an object is an instance of a class in C#check whether an object is an instance of a class in C#, use the is keyword:

var item = inventory.getItem( ... );

if ( item is SpecialItem ) {
    var specialItem = (SpecialItem) item;
    // do something special...
}

or just cast it with as and see if it succeeds:

var specialItem = item as SpecialItem;
if ( specialItem != null ) {
    // do something special...
}

See also: How to: Safely Cast by Using as and is Operators (C# Programming Guide)


Edit: To keep track of your items, you should organize them into a reasonable class hierarchy instead of having all of them inherit directly from BaseItem. So, for example, a helmet might be an instance of class Helmet, which extends Headgear, which extends Armor, which extends Wearable (armor, jewelry, etc.) which extends BaseItem. (Of course, you don't have to do it exactly like this. For example, if you have no use for a separate class for headgear, you can just have Helmet inherit directly from Armor.)

You'll probably also want to use interfaces to describe different aspects an item might have. For example, imagine that you want to have a magic helmet. Obviously, you could have a parent class MagicItem for all magic items, but C# won't let you inherit from both Helmet and MagicItem at the same time. Instead, you can define an interface IMagicItem that any item class can implement to indicate that it's magic (and provide methods to invoke its magic effects), and declare your magic helmet class like this:

public class MagicHelmet: Helmet, IMagicItem {
    // Implement the IMagicItem interface here, and optionally override some
    // methods from Helmet.
}

You could also, for example, define an IWearable interface in addition to (or instead of) the Wearable class, so that when you decide to, say, implement a flower that can be worn on the head (or maybe a tame parrot that can be worn on the shoulder), you can do that even though neither Flower nor Parrot are subclasses of Wearable.

(Ps. I had to go AFK for a while as I was writing this edit, and I see from the comments that you managed to solve your real problem in the meanwhile. I'll post this anyway, since it might still be helpful.)

To check whether an object is an instance of a class in C#, use the is keyword:

var item = inventory.getItem( ... );

if ( item is SpecialItem ) {
    var specialItem = (SpecialItem) item;
    // do something special...
}

or just cast it with as and see if it succeeds:

var specialItem = item as SpecialItem;
if ( specialItem != null ) {
    // do something special...
}

See also: How to: Safely Cast by Using as and is Operators (C# Programming Guide)


Edit: To keep track of your items, you should organize them into a reasonable class hierarchy instead of having all of them inherit directly from BaseItem. So, for example, a helmet might be an instance of class Helmet, which extends Headgear, which extends Armor, which extends Wearable (armor, jewelry, etc.) which extends BaseItem. (Of course, you don't have to do it exactly like this. For example, if you have no use for a separate class for headgear, you can just have Helmet inherit directly from Armor.)

You'll probably also want to use interfaces to describe different aspects an item might have. For example, imagine that you want to have a magic helmet. Obviously, you could have a parent class MagicItem for all magic items, but C# won't let you inherit from both Helmet and MagicItem at the same time. Instead, you can define an interface IMagicItem that any item class can implement to indicate that it's magic (and provide methods to invoke its magic effects), and declare your magic helmet class like this:

public class MagicHelmet: Helmet, IMagicItem {
    // Implement the IMagicItem interface here, and optionally override some
    // methods from Helmet.
}

You could also, for example, define an IWearable interface in addition to (or instead of) the Wearable class, so that when you decide to, say, implement a flower that can be worn on the head (or maybe a tame parrot that can be worn on the shoulder), you can do that even though neither Flower nor Parrot are subclasses of Wearable.

(Ps. I had to go AFK for a while as I was writing this edit, and I see from the comments that you managed to solve your real problem in the meanwhile. I'll post this anyway, since it might still be helpful.)

To check whether an object is an instance of a class in C#, use the is keyword:

var item = inventory.getItem( ... );

if ( item is SpecialItem ) {
    var specialItem = (SpecialItem) item;
    // do something special...
}

or just cast it with as and see if it succeeds:

var specialItem = item as SpecialItem;
if ( specialItem != null ) {
    // do something special...
}

See also: How to: Safely Cast by Using as and is Operators (C# Programming Guide)


Edit: To keep track of your items, you should organize them into a reasonable class hierarchy instead of having all of them inherit directly from BaseItem. So, for example, a helmet might be an instance of class Helmet, which extends Headgear, which extends Armor, which extends Wearable (armor, jewelry, etc.) which extends BaseItem. (Of course, you don't have to do it exactly like this. For example, if you have no use for a separate class for headgear, you can just have Helmet inherit directly from Armor.)

You'll probably also want to use interfaces to describe different aspects an item might have. For example, imagine that you want to have a magic helmet. Obviously, you could have a parent class MagicItem for all magic items, but C# won't let you inherit from both Helmet and MagicItem at the same time. Instead, you can define an interface IMagicItem that any item class can implement to indicate that it's magic (and provide methods to invoke its magic effects), and declare your magic helmet class like this:

public class MagicHelmet: Helmet, IMagicItem {
    // Implement the IMagicItem interface here, and optionally override some
    // methods from Helmet.
}

You could also, for example, define an IWearable interface in addition to (or instead of) the Wearable class, so that when you decide to, say, implement a flower that can be worn on the head (or maybe a tame parrot that can be worn on the shoulder), you can do that even though neither Flower nor Parrot are subclasses of Wearable.

(Ps. I had to go AFK for a while as I was writing this edit, and I see from the comments that you managed to solve your real problem in the meanwhile. I'll post this anyway, since it might still be helpful.)

added 1874 characters in body
Source Link
Ilmari Karonen
  • 8.4k
  • 1
  • 31
  • 39

To check whether an object is an instance of a class in C#, use the is keyword:

var item = inventory.getItem( ... );

if ( item is SpecialItem ) {
    var specialItem = (SpecialItem) item;
    // do something special...
}

or just cast it with as and see if it succeeds:

var specialItem = item as SpecialItem;
if ( specialItem != null ) {
    // do something special...
}

See also: How to: Safely Cast by Using as and is Operators (C# Programming Guide)


Edit: To keep track of your items, you should organize them into a reasonable class hierarchy instead of having all of them inherit directly from BaseItem. So, for example, a helmet might be an instance of class Helmet, which extends Headgear, which extends Armor, which extends Wearable (armor, jewelry, etc.) which extends BaseItem. (Of course, you don't have to do it exactly like this. For example, if you have no use for a separate class for headgear, you can just have Helmet inherit directly from Armor.)

You'll probably also want to use interfaces to describe different aspects an item might have. For example, imagine that you want to have a magic helmet. Obviously, you could have a parent class MagicItem for all magic items, but C# won't let you inherit from both Helmet and MagicItem at the same time. Instead, you can define an interface IMagicItem that any item class can implement to indicate that it's magic (and provide methods to invoke its magic effects), and declare your magic helmet class like this:

public class MagicHelmet: Helmet, IMagicItem {
    // Implement the IMagicItem interface here, and optionally override some
    // methods from Helmet.
}

You could also, for example, define an IWearable interface in addition to (or instead of) the Wearable class, so that when you decide to, say, implement a flower that can be worn on the head (or maybe a tame parrot that can be worn on the shoulder), you can do that even though neither Flower nor Parrot are subclasses of Wearable.

(Ps. I had to go AFK for a while as I was writing this edit, and I see from the comments that you managed to solve your real problem in the meanwhile. I'll post this anyway, since it might still be helpful.)

To check whether an object is an instance of a class in C#, use the is keyword:

var item = inventory.getItem( ... );

if ( item is SpecialItem ) {
    var specialItem = (SpecialItem) item;
    // do something special...
}

or just cast it with as and see if it succeeds:

var specialItem = item as SpecialItem;
if ( specialItem != null ) {
    // do something special...
}

See also: How to: Safely Cast by Using as and is Operators (C# Programming Guide)

To check whether an object is an instance of a class in C#, use the is keyword:

var item = inventory.getItem( ... );

if ( item is SpecialItem ) {
    var specialItem = (SpecialItem) item;
    // do something special...
}

or just cast it with as and see if it succeeds:

var specialItem = item as SpecialItem;
if ( specialItem != null ) {
    // do something special...
}

See also: How to: Safely Cast by Using as and is Operators (C# Programming Guide)


Edit: To keep track of your items, you should organize them into a reasonable class hierarchy instead of having all of them inherit directly from BaseItem. So, for example, a helmet might be an instance of class Helmet, which extends Headgear, which extends Armor, which extends Wearable (armor, jewelry, etc.) which extends BaseItem. (Of course, you don't have to do it exactly like this. For example, if you have no use for a separate class for headgear, you can just have Helmet inherit directly from Armor.)

You'll probably also want to use interfaces to describe different aspects an item might have. For example, imagine that you want to have a magic helmet. Obviously, you could have a parent class MagicItem for all magic items, but C# won't let you inherit from both Helmet and MagicItem at the same time. Instead, you can define an interface IMagicItem that any item class can implement to indicate that it's magic (and provide methods to invoke its magic effects), and declare your magic helmet class like this:

public class MagicHelmet: Helmet, IMagicItem {
    // Implement the IMagicItem interface here, and optionally override some
    // methods from Helmet.
}

You could also, for example, define an IWearable interface in addition to (or instead of) the Wearable class, so that when you decide to, say, implement a flower that can be worn on the head (or maybe a tame parrot that can be worn on the shoulder), you can do that even though neither Flower nor Parrot are subclasses of Wearable.

(Ps. I had to go AFK for a while as I was writing this edit, and I see from the comments that you managed to solve your real problem in the meanwhile. I'll post this anyway, since it might still be helpful.)

Source Link
Ilmari Karonen
  • 8.4k
  • 1
  • 31
  • 39

To check whether an object is an instance of a class in C#, use the is keyword:

var item = inventory.getItem( ... );

if ( item is SpecialItem ) {
    var specialItem = (SpecialItem) item;
    // do something special...
}

or just cast it with as and see if it succeeds:

var specialItem = item as SpecialItem;
if ( specialItem != null ) {
    // do something special...
}

See also: How to: Safely Cast by Using as and is Operators (C# Programming Guide)