I like this type of ElementAtOrDefaultOperator to use with generic lists, but I can not understand this How to define a default type What I understand for my list of items, the default value can be zero if I have a list of the items below, but I return my version of my default object with the appropriate values Want to do Here's what I mean:
class A {string field A; String field B; Class B field C; // constructor} list & lt; Class A & gt; MyObjects = New list & lt; ClassA & gt; (); MyObjects.Add (// new object) myObjects.Add (// new object)
I want to be able to do the following:
class A new object = myObjects.ElementAtOrDefault (3);
And the new object is one of the primary types that I define elsewhere. I thought there might be a SetDefaultElement or any other method but I do not think it exists.
Any thoughts?
Just type your own extension method:
Fixed T elements At or default & lt; T & gt; (This is iLIT & lt; T & gt; list, int index, T @ default) {return index & gt; = 0 & amp; Amp; Index & lt; List List [index]: @default; }
and call:
var items = myObjects.ElementAtOrDefault (3, defaultItem);
Or if you want to be evaluated by default, then Func & lt; T & gt; Use
:
Fixed TiElementEyour Default & lt; T & gt; (This is iLIT & lt; T & gt; list, int index, funk & lt; T & gt; @default) {return index & gt; = 0 & amp; Amp; Index & lt; List List [index]: @ default (); }
This form will allow you to use a pre-defined item (for example, for example), for example:
var Item = myObjects Element atoffault (3, myObjects.First);
Or more flexible:
var items = myObjects.ElementAtOrDefault (3, () => myobject [2]);
or
var items = myObjects.ElementAtOrDefault (3, () => New MyObject {...});
Comments
Post a Comment