c# - Shorter syntax for List construction -


with c# 6.0, given static method:

public static list<t> list<t>(params t[] items) => new list<t>(items); 

and appropriate using:

using static listtest.listutils; 

i can use list construct lists:

var lsint = list(10, 20, 30);  var lsstr = list("abc", "bcd", "cde"); 

without list method, construction can done via:

var lsintinitializer = new list<int>() { 10, 20, 30 }; 

the name list being used both class , method. seems work fine, besides not being considered idiomatic, there technical reasons why shouldn't use list method name here?

i can use list, that's not idiomatic in different way, in methods tend capitalized.

note, have no problems going non-idiomatic route, if there's accepted practice in area, i'd know it.

of course, c# 6.0 using static brave new world, perhaps there's not yet enough community experience around sort of thing.

the language-ext project uses similar approach construct immutablelist objects. in case, use name list.

an entire working example above excerpts taken shown below.

using system; using system.collections.generic;  using static listtest.listutils;  namespace listtest {     public static class listutils     {         public static list<t> list<t>(params t[] items) => new list<t>(items);     }     class program     {         static void main(string[] args)         {             var lsint = list(10, 20, 30);              var lsstr = list("abc", "bcd", "cde");              var lsintinitializer = new list<int>() { 10, 20, 30 };              lsint.foreach(console.writeline);              lsstr.foreach(console.writeline);         }     } } 

perhaps less confusing approach this:

public static class list {     public static list<t> create<t>(params t[] items) => new list<t>(items); } 

which call this:

var lsint = list.create(10, 20, 30); 

(without using static of course; importing class method named create really confusing)

this same approach used immutable collections (immutablelist, immutablearray, etc)


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -