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 401465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:02:11+00:00 2026-05-12T17:02:11+00:00

I’ve written my own custom data layer to persist to a specific file and

  • 0

I’ve written my own custom data layer to persist to a specific file and I’ve abstracted it with a custom DataContext pattern.

This is all based on the .NET 2.0 Framework (given constraints for the target server), so even though some of it might look like LINQ-to-SQL, its not! I’ve just implemented a similar data pattern.

See example below for example of a situation that I cannot yet explain.

To get all instances of Animal – I do this and it works fine

public static IEnumerable<Animal> GetAllAnimals() {
        AnimalDataContext dataContext = new AnimalDataContext();
            return dataContext.GetAllAnimals();
}

And the implementation of the GetAllAnimals() method in the AnimalDataContext below

public IEnumerable<Animal> GetAllAnimals() {
        foreach (var animalName in AnimalXmlReader.GetNames())
        {
            yield return GetAnimal(animalName);
        }
}

The AnimalDataContext implements IDisposable because I’ve got an XmlTextReader in there and I want to make sure it gets cleaned up quickly.

Now if I wrap the first call inside a using statement like so

public static IEnumerable<Animal> GetAllAnimals() {
        using(AnimalDataContext dataContext = new AnimalDataContext()) {
            return dataContext.GetAllAnimals();
        }
}

and put a break-point at the first line of the AnimalDataContext.GetAllAnimals() method and another break-point at the first line in the AnimalDataContext.Dispose() method, and execute…

the Dispose() method is called FIRST so that AnimalXmlReader.GetNames() gives “object reference not set to instance of object” exception because AnimalXmlReader has been set to null in the Dispose() ???

Any ideas? I have a hunch that its related to yield return not being allowed to be called inside a try-catch block, which using effectively represents, once compiled…

  • 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-05-12T17:02:12+00:00Added an answer on May 12, 2026 at 5:02 pm

    When you call GetAllAnimals it doesn’t actually execute any code until you enumerate the returned IEnumerable in a foreach loop.

    The dataContext is being disposed as soon as the wrapper method returns, before you enumerate the IEnumerable.

    The simplest solution would be to make the wrapper method an iterator as well, like this:

    public static IEnumerable<Animal> GetAllAnimals() {
        using (AnimalDataContext dataContext = new AnimalDataContext()) {
            foreach (var animalName in dataContext.GetAllAnimals()) {
                yield return GetAnimal(animalName);
            }
        }
    }
    

    This way, the using statement will be compiled in the outer iterator, and it will only be disposed when the outer iterator is disposed.

    Another solution would be to enumerate the IEnumerable in the wrapper. The simplest way to do that would be to return a List<Animal>, like this:

    public static IEnumerable<Animal> GetAllAnimals() {
        using (AnimalDataContext dataContext = new AnimalDataContext()) {
            return new List<Animal>(dataContext.GetAllAnimals());
        }
    }
    

    Note that this loses the benefit of deferred execution, so it will get all of the animals even if you don’t need them.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
i want to parse a xhtml file and display in UITableView. what is the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.