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

  • Home
  • SEARCH
  • 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 8087829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:54:18+00:00 2026-06-05T18:54:18+00:00

I am making a base class that has a virtual method called GetBaseAddresses(). It

  • 0

I am making a base class that has a virtual method called “GetBaseAddresses()”. It has as a return type IEnumerable<Uri>. The base class will not yield any results if enumerated, but a derived class has the option to override the method and return as many items as it wants.

Here is the base method:

public virtual IEnumerable<Uri> GetBaseAddresses() { }

The problem is, that won’t compile. You must return a value for the compiler to be happy. So, because I want to have an empty result, I would just return null, right?

public virtual IEnumerable<Uri> GetBaseAddresses() { return null; }

The problem there is that if someone does a foreach on an instance of the base class, they will crash with a runtime error of “Object reference not set…”

So, recalling that the yield return keyword does some magic with the C# compiler… I came up with this compiler hack (that works by the way).

public virtual IEnumerable<Uri> GetBaseAddresses()
{
    if (false) { yield return new Uri(""); }
}

Strangely enough, even though the “if (false) { … }” code gets completely compiled away – the compiler is happy with me meeting the “must return a value” requirement, and does exactly what I want – that is, an empty result set that is safe to enumerate.

My question is – Is there a way to do this without my compiler trick?

  • 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-05T18:54:19+00:00Added an answer on June 5, 2026 at 6:54 pm

    Just return an empty enumerable in the base method.

        public virtual IEnumerable<Uri> GetBaseAddresses()
        {
            return Enumerable.Empty<Uri>();
        }
    

    Or if you’re targeting a version of the .NET framework < 3.5 return an empty List.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class and it has virtual functions. The issue is that,
I am making an abstract-base-class and was thinking I might want a pure virtual
I'm interested in making an immutable class that has properties that cannot be modified,
I have a WeekdayException class that has the ToString(). I want to return the
I'd like to have a base class that has a constant field (like an
I'm making this method retrieve records from the Data Base. As you can see
So, I have a base class which has a private locking object like so:
I have an abstract immutable base class that defines enforces child classes to be
I have some code that has a dynamic-class system in C++ that has a
I'm looking to use the UpdateModel method to a Sub Class that retrieved at

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.