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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:51:19+00:00 2026-05-28T03:51:19+00:00

Is there anyway to achieve that without loading the whole file into memory? If

  • 0

Is there anyway to achieve that without loading the whole file into memory? If so, what do you suggest me to do?

Class implementation:

[Serializable()]
public class Car
{
    public string Brand { get; set; }
    public string Model { get; set; }
}

[Serializable()]
public class CarCollection : List<Car>
{
}

Serialization to file:

CarCollection cars = new CarCollection
{
    new Cars{ Brand = "BMW", Model = "7.20" },
    new Cars{ Brand = "Mercedes", Model = "CLK" }
};

using (Stream stream = File.Open("data", FileMode.Create))
{
    BinaryFormatter bin = new BinaryFormatter();
    bin.Serialize(stream, cars);
}
  • 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-28T03:51:20+00:00Added an answer on May 28, 2026 at 3:51 am

    To deserialize the collection one object at a time, you also need to serialize it one at a time.

    Simplest way is to define your own generic class:

    public static class StreamSerializer
    {
        public static void Serialize<T>(IList<T> list, string filename)
        {
            using (Stream stream = File.Open(filename, FileMode.Create))
            {
                BinaryFormatter bin = new BinaryFormatter();
    
                // seralize each object separately
                foreach (var item in list)
                    bin.Serialize(stream, item);
            }
        }
    
        public static IEnumerable<T> Deserialize<T>(string filename)
        {
            using (Stream stream = File.Open(filename, FileMode.Open))
            {
                BinaryFormatter bin = new BinaryFormatter();
    
                // deserialize each object separately, and 
                // return them one at a time
    
                while (stream.Position < stream.Length)
                    yield return (T)bin.Deserialize(stream);
            }
        }
    }
    

    Then you can simply write:

    CarsCollection cars = new CarsCollection
    {
        new Cars{ Brand = "BMW", Model = "7.20" },
        new Cars{ Brand = "Mercedes", Model = "CLK" }
    };
    
    // note that you cannot serialize the entire list if
    // you want to query without loading - it must be symmetrical
    
    StreamSerializer.Serialize(cars, "data.bin");
    
    // the following expression iterates through objects, processing one 
    // at a time. "First" method is a good example because it
    // breaks early.
    
    var bmw = StreamSerializer
        .Deserialize<Cars>("data.bin")
        .First(c => c.Brand == "BMW");
    

    A slightly more complex case might be if your CarsCollection belongs to a different class. In that case, you will need to implement ISerializable, but the principle is similar.

    On a side note, usual convention is not to name entities in plural (i.e. Cars should be named Car).

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

Sidebar

Related Questions

Is there anyway to combine all resources into a single exe file such as
Is there anyway to declare an object of a class before the class is
Is there any way to make a code like this work: public class Func2<A,
Is there any way to achieve function overloading in C? I am looking at
I would like to ask is there any way to achieve this functionality: I
Is there anyway to have a sort of virtual static member in C++? For
Is there anyway to configure a WCF service with a failover endpoint if the
Is there anyway in Java to delete data (e.g., a variable value, object) and
Is there anyway to upgrade the installed JRE in the system? We are having
Is there anyway to build a solution to target 64 bit environment in vs2003?

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.