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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:17:12+00:00 2026-05-21T03:17:12+00:00

Suppose you have a class Person : public class Person { public string Name

  • 0

Suppose you have a class Person :

public class Person
{
   public string Name { get; set;}
   public IEnumerable<Role> Roles {get; set;}
}

I should obviously instantiate the Roles in the constructor.
Now, I used to do it with a List like this :

public Person()
{
   Roles = new List<Role>();
}

But I discovered this static method in the System.Linq namespace

IEnumerable<T> Enumerable.Empty<T>();

From MSDN:

The Empty(TResult)() method caches an
empty sequence of type TResult. When
the object it returns is enumerated,
it yields no elements.

In some cases, this method is useful
for passing an empty sequence to a
user-defined method that takes an
IEnumerable(T). It can also be used to
generate a neutral element for methods
such as Union. See the Example section
for an example of this use of

So is it better to write the constructor like that? Do you use it? Why? or if not, Why not?

public Person()
{
   Roles = Enumerable.Empty<Role>();
}
  • 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-21T03:17:13+00:00Added an answer on May 21, 2026 at 3:17 am

    I think most postings missed the main point. Even if you use an empty array or empty list, those are objects and they are stored in memory. The Garbage Collector has to take care of them. If you are dealing with a high throughput application, it could be a noticeable impact.

    Enumerable.Empty does not create an object per call thus putting less load on the GC.

    If the code is in low-throughput location, then it boils down to aesthetic considerations though.

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

Sidebar

Related Questions

Suppose I have one class Person ……… public class Person { public string Name
Say Suppose you have a class public class Person { public int PesronId{get;set;} public
Suppose I have a class class person { char* name; public: void setname(const char*);
Say suppose I have the following Java code. public class Example { public static
Suppose I have a Django model as follows: class Person(models.Model): first_name = models.CharField(max_length=50) last_name
Suppose I have Dictionary like this: Dictionary<string, string> values = new Dictionary<string, string>() {
Below I have a Person interface, an implementing class and a driver class which
If I have a simple list of Strings: List<String> stringList = new ArrayList<String>(); I
I have a base class which implements the == operator. I want to write
I have a Person object with two constructors - one takes an int (personId),

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.