1

Good day! This question seems easy, but I can't think out how to do it. I would pass Type parameter to function and check whether variable has that type.

void foo(Type type = ) // how to pass here default value for example System.Object?
{       
        if (elem.GetType() is type) 
        {

        }
    }
}

EDIT I have next classes

class Element {}
class ChildElementClass : Element {}
class SecondChildEleementClass : Element{}

And have array of Elements[], which is storing elements of all three classes

Element[] elements;

So, I would retrieve all elements for ChildElementClass. I make it by other way, but it is just interesting.

2 Answers2

5

Use this:

void foo(Type type = null)
{
    if (type == null)
        type = typeof(object);
}

See: here

PinBack
  • 2,499
  • 12
  • 16
2

you could create a helper method to pass a default value

public void foo(){
   foo(typeof(object));
}
private void foo(Type type){
   //
}

or, set the default type as null and specify a default inside the method

if (type == null) {
    type = typeof(object);
}
jasttim
  • 723
  • 8
  • 19