Skip to main content
0 votes
0 answers
32 views

Pass `string[][]` as `InlineData to xUnit Theory? [duplicate]

In an xUnit theory I'd like to send an InlineData parameter of type string[][]. I can't seem to get it right. Is it doable, and if so, how? I would expect this to work at least: [InlineData(new string[...
Kjell Rilbe's user avatar
  • 1,623
1 vote
0 answers
51 views

CS0121 Ambiguous Call with Collection Initializer [duplicate]

I'm on a team with two other devs. For the past few months I've been working on a project in C# 12 independently, and only recently have the other devs pulled my code and run it. For one of them it ...
mdirks's user avatar
  • 73
3 votes
1 answer
588 views

ImmutableArray empty collection initializer is not equal to ImmutableArray.Empty in C# 12

It appears that C# 12's collection initializers don't produce the same result as a ImmutableArray's Empty property; for example: using System; using System.Collections.Immutable; ...
Matthew Layton's user avatar
2 votes
2 answers
2k views

What's the syntax for using C#12 collection expressions to initialise a List<List>>?

Say I have code like this (or any kind of jagged structure): var intListList = new List<List<int>> { new() { 1, 2, 3 } }; I'd like to use a collection expression here. Resharper (EAP) ...
stuartd's user avatar
  • 73.6k
30 votes
2 answers
2k views

What does assignment to a bracketed expression mean in C#?

I'm reading Avalonia source code and I came across this sentence: return new MenuFlyoutPresenter { [!ItemsControl.ItemsProperty] = this[!ItemsProperty], [!ItemsControl.ItemTemplateProperty] = ...
Aleksander Stukov's user avatar
1 vote
2 answers
663 views

Supporting collection initialization syntax for immutable collections - init modifiers for methods?

I need to implement an immutable collection which supports collection initialization syntax. The problem is in order to support collection initialization syntax, the collection must provide ...
Dmitry Kotleev's user avatar
1 vote
2 answers
70 views

Set inner object's member as outer object using the object initialization syntax in C#

Context I have a List<T> of type Question. Class Question, in turn, contains a List<Answer>. Class Answer has a member called public Question Question { get; set; } which stores the ...
Amal K's user avatar
  • 4,988
1 vote
2 answers
339 views

Why is a collection initializer without `new` allowed inside an object initializer but not outside?

I noticed strange behaviour when initializing collection property. Consider: class X { public IList<int> Ints { get; set; } } I can initialize Ints like that: var theObject = new X { ...
Pawel's user avatar
  • 624
2 votes
1 answer
573 views

Collection initializers inside object initializers with default values

I just stumbled upon the following issue: class Settings { // Let's set some default value: { 1 } public ICollection<int> AllowedIds = new List<int>() { 1 }; } static void Main(...
Heinzi's user avatar
  • 173k
1 vote
1 answer
829 views

Use of C# 8 null-forgiving operator within C# 6 index initializers

When using an index initializer to fill a dictionary without recreating it before that, is there an option to specify the null-forgiving operator somewhere? Example: public class Program { ...
mxscho's user avatar
  • 2,190
1 vote
2 answers
144 views

Can't dynamically set array initializer length

I am using C# to hook into the FedEx API and I'm a bit stumped on how to modify some existing code to meet my needs. The snippet included is part of their canned code where they sample how to work ...
user1134307's user avatar
0 votes
2 answers
2k views

Initializing a list of lambda expressions

I'm designing a state machine class and want to use lambda expressions to represent conditions to satisfy the state transition objects. When I create a new State Transition object, I also want to pass ...
Jason O's user avatar
  • 833
10 votes
4 answers
6k views

Initialize elements with brackets like Lists in c#

I was thinking about arrays and lists and wondering if and how classes can get an implementation to be initializable like them. Let's take this class as basis: class TestClass { private List<...
Battle's user avatar
  • 826
7 votes
1 answer
1k views

C# List or Collection initializer using foreach loop

Is it possible in C# to do something similar to the following? var tigerlist = new List<Tigers>(){ Tail = 10, Teeth = 20 }; var tigers_to_cats_approximation = new List<Cat>() { ...
Watson's user avatar
  • 1,425
3 votes
3 answers
506 views

C# Collection initialization syntax for use with custom matrix class?

I'm creating a custom matrix class that only has a single 2D array to hold everything (I know that a 1D array is faster and better, but that's not the point of this implementation) the thing is, I'd ...
Raxmo's user avatar
  • 57

15 30 50 per page