Saher Ahwal
Saher Ahwal

Reputation: 9237

Checking Type Inside Collection in C#

I am kind of new to C# and not sure about the following: I have a parameter defined as follows:

public Collection<object> InputObject
        {
            get { return inputObject; }
            set { inputObject= value; }
        }
        private Collection<object> inputObject;

As you can see, the object is a collection of objects since I am expecting different types.

When later I want to check the type, what is the right way? I tried this:

if (inputObject is Collection<MyDefinedObject>)
{

}

but I get an underlined warning that the given expression is never of the expected type. What is the right way to check for a type within a collection?

Thanks

Upvotes: 2

Views: 502

Answers (6)

phoog
phoog

Reputation: 43036

You say that the inputObject property can contain collections whose collection type can vary from time to time. There are two solutions:

1) If the type of the collection's elements is always the same for a given instance of the type that defines the property, make that type generic:

public class MyClass<T>
{
    public Collection<T> InputObject 
    { 
        get { return inputObject; } 
        set { inputObject= value; } 
    } 
    private Collection<T> inputObject; 
}

If the same instance of the property could hold collections with varying element types, then declare the field as object, or, perhaps better, as a non-generic interface type:

public class MyClass
{
    public ICollection InputObject
    {
        get { return inputObject; } 
        set { inputObject= value; }  // you should add some type checking here 
                                     //to make sure an invalid collection type isn't passed in
    }
    private ICollection inputObject;
    public Collection<T> GetTypedCollection<T>()
    {
        return (Collection<T>)inputObject; 
    }
}

The GetTypedCollection method requires that you know at compile time what the collection's type is; if that's not possible, you'll need to post more sample code so we can suggest a solution.

Upvotes: 0

Mare Infinitus
Mare Infinitus

Reputation: 8162

Perhaps this is what you want:

        var defined = InputObject.OfType<MyDefinedObject>();
        var other = InputObject.OfType<MyOtherDefinedObject>();

        bool hasDefined = defined.Any();
        bool hasOther = other.Any();

Upvotes: 0

Eric J.
Eric J.

Reputation: 150108

You have a collection of Object

When you write

Collection<object>

that means you are defining a collection that can contain any object.

When you do that, you can then do something like

inputObject.Add(new MyDefinedObject());
inputObject.Add(new StringBuilder()));

and you end up with different types in your collection.

Checking specific elements of your collection

If you want to test the type of a specific object in that collection you could so something like

Type t = inputObject[0].GetType();

or

if (inputObject[0] is MyDefinedObject)

The code

if (inputObject is Collection<MyDefinedObject>)

will never be true because you already defined

private Collection<object> inputObject;

meaning you defined the type of the collection inputObject to be Collection<object>

What if I want everything in the collection to be MyDefinedObject?

Based on your comments below your question, this is what you really want...

private Collection<MyDefinedObject> inputObject;

The Error Message You are Getting

but I get an underlined warning that the given expression is never of the expected type

That is because inputObject is of type Collection<object>, which is never of type Collection. That's like saying an apple is never a banana.

A Better Approach

If you are using generics and receiving a generic collection, why not create a generic function to process that collection too?

private void ProcessCollection<T>(Collection<T> inputObject)
{
}

// Somewhere else in your code...

Collection<MyDefinedObject> inputObject = // Initialize this however you do now
ProcessCollection<MyDefinedObject>(inputObject);

Upvotes: 4

paparazzo
paparazzo

Reputation: 45096

You are going to need to check all. The collection is defined as object

foreach (object obj in InputObject)
{
    obj.gettype();     
    if (obj is DocFieldEnumHierTwoSpeedLoader) x= 5;
}

Upvotes: 1

XhkUnlimit
XhkUnlimit

Reputation: 353

All object inherits the base class object, then you can use :

if(InputObject[0].GetType()==typeof(string)){
//string is an example and 0 can be replaced with a index
}

to know which type is each object

Upvotes: 1

Stu
Stu

Reputation: 15769

Since your Collection<> contains object, the collection has (in essence) no type. You either give the collection an actual type, or you check the items IN the collection for a type.

Upvotes: 2

Related Questions