Collection Initializer Extension Methods - CSharp Custom Type

CSharp examples for Custom Type:Extension Methods

Description

Collection Initializer Extension Methods

Demo Code

using System;//from   www  .  j a  v a 2 s.  com
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
static class StringListExtensions
{
    public static void Add(this List<string> list, int value, int count = 1)
    {
        list.AddRange(Enumerable.Repeat(value.ToString(), count));
    }
}
class CollectionInitializerExtensionMethods
{
    static void Main()
    {
        List<string> strings = new List<string>
      {
         10,
         "hello",
         { 20, 3 }
      };
        Console.WriteLine(strings.Count);
    }
}

Result


Related Tutorials