Saturday, June 1, 2019

c# - issues customizing List into ListCustomMinIndexedForEach (stripped from irelevant implementations)

I tried to do my best to keep the title short but still informative.



I think I have succeeded with most of it



my problem For Now:(TLDR below)



I could not succeed implementing a public void method-member, nor an extension to Implement custom ForEach() with index..



as Extension




    public static ListWithCounter ForEach(this  ListWithCounter Self, Action itm)//, ListWithCounter l = l.CurId)
{

for (int i = 0; i < Self.Count; i++)
{
Self.CurId = i;
itm(Self[i]);Self.CurId++;

}
return Self;

}


this is a small issue I guess, the objectives :




  • Create Customized List No Fancy Extra(Expensive) methods


  • add an elegant ForEach implementation


  • optional tricks like out of the box GetEnumValues/names . To{ourCustomList}()




USAGE



in this example I am using an Enum ( names of Actions also used to present action items in console menu)



public enum ActSrptS { CreateMmfTfomFile_DoFormat, OpenExitMmfT, .... }


so I print it to console by unleashing the power of That small List class




                                    //a struct ConsoleModifiers + ConsoleKey
ActScrptS aAction = ActScrptS._Start; Combination Comb = new Combination();
var actS = aAction._EnmGetValues();
var actSNms = aAction.EnumGetNamesToList().ForEach(Act =>
{
Console.WriteLine("[{0}]{1}", actS.CurId, Act);
});
Console.WriteLine("===============\r\n");
Console.WriteLine("please Select Action");



and it's simply using (TRYING without success for now..)



public static ListWithCounter EnumGetNamesToList(this Enum selfEnum)
{
return selfEnum.GetType().GetFields(BindingFlags.Static | BindingFlags.Public)
.Select(f=>f.Name).ToList();
//var values = Enum.GetNames(typeof(selfEnum)).ToList();
//return values;
//var values = Enum.GetValues(typeof(Environment.SpecialFolder)).Cast().ToList();


}



public static ListWithCounter _EnmGetValues(this Enum Self)
{
ListWithCounter enumerations = new ListWithCounter();
foreach (FieldInfo fieldInfo in Self.GetType().GetFields(
BindingFlags.Static | BindingFlags.Public))

{
enumerations.Add((Enum)fieldInfo.GetValue(Self));
}
return enumerations;
}


so I started with MSDN List.cs



and I tried to implement as less methods as possible





  • leaving minimal important functionality

  • altering Growth/Expand for -minimal copying so staring capacity is from say 10-50, multiplied by *4 on each limit...



came out with this CODE

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...