Let's just say I'd like to iterate over a string[][]
, append a value using an anonymous type and perform a generic ForEach-Extensionmethod on the result (brilliant example, I know, but I suppose you'll get the jist of it!).
Here's my code:
//attrs = some string[][]
attrs.Select(item => new { name = HttpContext.GetGlobalResourceObject("Global", item[0].Remove(0, 7)), value = item[1] })
.ForEach</*????*/>(/*do stuff*/);
What exactly would I put in the Type-Parameter of ForEach?
Here's what ForEach looks like:
public static void ForEach<T>(this IEnumerable<T> collection, Action<T> act)
{
IEnumerator<T> enumerator = collection.GetEnumerator();
while (enumerator.MoveNext())
act(enumerator.Current);
}
Vincent Diamante (CC BY-SA 2.0) An anonymous type is a type that doesn't have a name. You can use an anonymous type to encapsulate a set of read-only properties inside a single unit — and you don't need to define the anonymous type beforehand.
To call a generic method, you need to provide types that will be used during the method invocation. Those types can be passed as an instance of NType objects initialized with particular . NET types.
Generic methods in non-generic classYes, you can define a generic method in a non-generic class in Java.
Anonymous types in C# are the types which do not have a name or you can say the creation of new types without defining them. It is introduced in C# 3.0. It is a temporary data type which is inferred based on the data that you insert in an object initializer.
You don't need to specify the type explicitly, because it can be inferred from the supplied parameters:
attrs.Select(item => new
{
name = HttpContext.GetGlobalResourceObject("Global",
item[0].Remove(0, 7)),
value = item[1]
})
.ForEach(x => x.name = "something");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With