tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

My C# array, tuple, delegate declaration dilemma

14 Nov 2022 2 mins C#

I usually create arrays like this. Nothing fancy. And most people around me use the same. I would even say it’s kind of a standard way in C#.

var data = new[] { 1, 2, 3 };

But today I realized, you can also use this (target-typed new expressions) when using arrays.

int[] data2 = { 1, 2, 3 };

Which looks dumb if you prefer var, like I do. And if you don’t, what’s wrong with you? 😎

Also, this doesn’t work if you want to pass that array into a method, for example.

void Foo(int[] data) { }
Foo({ 1, 2, 3 });

So, you might be wondering why I’m writing about it.

Well, there’s at least one case where this, approach works better than my usual one. Let’s say you have a static readonly array (kind of a constant) with tuples with delegates. Something like this.

float Bar(float x) => x;

static readonly (string, Func<float, float>)[] Data = new[]
{
	("test", Bar),
};

Then this declaration doesn’t work, and you have to explicitly state the type of array.

static readonly (string, Func<float, float>)[] Data2 = new (string, Func<float, float>)[]
{
	("test", Bar),
};

I’m not fan of this.

But what you actually can do, and works fine, is this.

static readonly (string, Func<float, float>)[] Data3 =
{
	("test", Bar),
};

Isn’t that cool (or weird)? I like that I don’t have to repeat the type. I like that a lot. On the other hand, I don’t like the missing new (I don’t like target-typed new expressions in general). But, but it’s so nice, succinct. I like succinct code. But I also like consistency in my code. In any code. Could I use it everywhere, i.e., for above mentioned arguments for methods, I would be probably sold (for arrays only).

I’m so torn right now. I might have to revisit my very own C#-code-writing rules 🤯.

Profile Picture Jiří Činčura is .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at www.tabsoverspaces.com.