Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8940899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:09:59+00:00 2026-06-15T11:09:59+00:00

I can work around this, but I am curious why it won’t work: In

  • 0

I can work around this, but I am curious why it won’t work:

In the same way that you can create an optional parameter with a default value for a routine, such as the one below…..

    public void SomeRoutine(string Title = "Missing")
    {
        // Do something with Title
    }

…. why can’t you assign a default Type as an optional parameter ?

The following example gives the error: “Default Parameter For ‘theType’ must be a compile time constant.”

    public void SomeOtherRoutine(Type theType = typeof(MyClass))
    {
        // Do something based on theType
    }

The actual application is trying to do provide the option for enumerating a collection that contains a mix of base and various derived classes, and returning only the class type of interest:

    public IEnumerable<MyBaseClass> EnumerateWithOptions(Type optionalDerivedClass = typeof(MyBaseClass))
    {
        foreach (MyBaseClass thingy in MyCustomCollection())
        {
            if (thingy.GetType() == optionalDerivedClass)
            { yield return thingy; };
        }
    }

The obvious alternative is to overload the routine to apply the default value as shown below, however it is not ideal in my app for reasons that are not worth trying to describe.

    public IEnumerable<MyBaseClass> EnumerateWithOptions()
    {
        return EnumerateWithOptions(typeof(MyBaseClass));
    }
    public IEnumerable<MyBaseClass> EnumerateWithOptions(Type optionalDerivedClass)
    {
        foreach (MyBaseClass thingy in MyCustomCollection())
        {
            if (thingy.GetType() == optionalDerivedClass)
            { yield return thingy; };
        }
    }

Any ideas why typeof(MyClass) is not considered a compile time constant, or any ideas for a different approach ? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:10:01+00:00Added an answer on June 15, 2026 at 11:10 am

    Thus you cannot use typeof or some Type value, and you don’t want to create overload manually, then consider provide null as default value for optional parameter:

    IEnumerable<MyBaseClass> EnumerateWithOptions(Type optionalDerivedClass = null)
    {
        if (optionalDerivedClass == null)
            return MyCustomCollection();
    
        return MyCustomCollection()
            .Where(thingy => thingy.GetType() == optionalDerivedClass);
    }
    

    If it is possible for you to use generic parameter instead of Type object, then you can filter collection via Enumerable.OfType<T>():

    IEnumerable<MyBaseClass> result = MyCustomCollection().OfType<YourType>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is easy to work around, but I just curious if I could be
I have some experience in ASP.Net and can work my way around it without
This is hopefully a quick/easy one. I know a way to work around this
Where multiple people can work on same css. is it possible to follow semantic
Can I bind my h:dataTable/rich:dataTable with some Map? I found that h:dataTable can work
I need some help modifying the Webform Module so that it can work for
Its a well known fact that a static method can work only on static
I'm trying to create a C++ class which can work as a holder for
I have a curious little problem. I have a service that needs to create
So, let's say that I have an element that is inline-block. So this won't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.