c# - How can i shorten this new object code? -
i know silly question , easy i'm staring screen 3 hours , still can't seem figure out. idea how shorten code? have public enum type
, public enum size
in class car.cs.
car car1 = new car((cartype)0, (size)0); car car2 = new car((cartype)0, (size)1); car car3 = new car((cartype)0, (size)2); car car4 = new car((cartype)1, (size)0); car car5 = new car((cartype)1, (size)1); car car6 = new car((cartype)1, (size)2); car car7 = new car((cartype)2, (size)0); car car8 = new car((cartype)2, (size)1); car car9 = new car((cartype)2, (size)2);
edit: print them method this
group1print(car1, car2, car3); group2print(car4, car5, car6); group3print(car7, car8, car9);
try this:
var cars = new list<car>(); foreach (type type in enum.getvalues(typeof(type))) { foreach (size size in enum.getvalues(typeof(size))) { cars.add(new car() {size = size, type = type}); } }
or linq version:
var cars = (from type type in enum.getvalues(typeof(type)) size size in enum.getvalues(typeof(size)) select new car() {size = size, type = type}).tolist();
wiki
Comments
Post a Comment