I am pretty new to C# and I see a lot of code where I'm not quite familiar with the syntax. So I was wondering if there's some simpler way of doing what I did here:
I have a class with various properties and functions. One of them is public int gettypeforitem(int i)
which returns an integer.
I want to define a property of type int[]
that returns an array of the types of all items.
I come from C++, so the following code seems logic to me, but I was wondering if there's a more "straight forward" way in doing this in C#. Thank you!
public int[] type
{
get
{
List<int> _list = new List<int>();
for(uint i=0; i<NumberOfItems;i++)
_list.Add(gettypeforitem(i));
return _list.ToArray();
}
}
LINQ is the way forward here, I'd say:
public int[] Types => Enumerable.Range(0, NumberOfItems)
.Select(i => GetTypeForItem(i))
.ToArray();
I've changed the names to follow .NET naming conventions, and this is using C# 6's expression-bodied property syntax.
As this is doing a relatively large amount of work for a property - generating a new array every call, for a start - you might want to consider making it a method instead:
public int[] GetTypes() => Enumerable.Range(0, NumberOfItems)
.Select(i => GetTypeForItem(i))
.ToArray();
As noted in comments elsewhere, you may be able to use a method group conversion for the argument to the Select
method:
public int[] GetTypes() => Enumerable.Range(0, NumberOfItems)
.Select(GetTypeForItem)
.ToArray();
The exact rules for when method group conversions are valid as arguments always elude me, so I won't try to summarise them here. (They've changed over time, too.)
See more on this question at Stackoverflow