I suggest you ...

Collection initializers for read-only collection properties in VB.NET

I'm very happy that Microsoft is committed to VB.NET have the same features as C#.
One feature I use extensivle in C# is collection initializers. I've just noticed that VB.NET supports these but not for read-only class properties.
I found at http://msdn.microsoft.com/en-us/library/dd293617.aspx that if a collection has a an Add method, it is used to initialize the variable. This should also apply to read-only collection properties.

Here is compilable C# code showing the feature working:

using System;
using System.Collections.Generic;

namespace ConsoleApplication3
{
class Class1
{
private readonly List<Int32> list = new List<Int32>();

public List<Int32> ListProp
{
get
{
return this.list;
}
}
}

class Program
{
static void Main(string[] args)
{
// a collection initialization
var list = new List<Int32> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

// a read-only collection property initialization
var class1 = new Class1
{
ListProp = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
};
}
}
}

Thanks,
Antao

4 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Antao AlmadaAntao Almada shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    1 comment

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Visual Studio teamAdminVisual Studio team (Product Team, Microsoft) commented  ·   ·  Flag as inappropriate

        Antao,

        Thanks for this awesome suggestion. It's a difference between the two languages that we've noticed but we haven't had a chance to revisit to fix. This won't make it in to the next release of VB, however. We're currently reviewing our wishlist of features we might like to tackle beyond the next release and we were planning to look at object/collection initializers in our next design meeting to see where we can improve. I'll definitely make sure we review this problem and see if we can come up with a design and a plan for fixing this in the future and let you know if we decide one way or another.

        Thanks again for raising this!

        Regards,

        Anthony D. Green | Program Manager | Visual Basic & C# Languages Team

      Feedback and Knowledge Base