52 questions
0
votes
0
answers
33
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[...
3
votes
1
answer
596
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;
...
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] = ...
1
vote
2
answers
75
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 ...
2
votes
1
answer
584
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(...
1
vote
2
answers
151
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 ...
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<...
3
votes
3
answers
512
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 ...
-2
votes
1
answer
72
views
function/method in a collection initializer c# [closed]
can i insert a method/function in a collection initializer? i am trying to insert expression in the function within a collection initializer but the variable in the class is asking for the declaration ...
-2
votes
1
answer
2k
views
Declaring a Dictionary<string,Func<>> with Functions already inside them c#
First of all, I don't even know what to use when passing on 4 parameters but no return value, I'll just use Func as an example.
I do not want to use Dictionary.Add to insert my function, I want it to ...
1
vote
2
answers
66
views
Initialization of list in class for availability from functions
I want initialize list with collection initializer values in class to make it available to use from separate functions:
public Form1()
{
InitializeComponent();
}
List<string> list = new ...
2
votes
1
answer
405
views
initialize collection-member on a class [duplicate]
I have a class with that has a collection-property:
class MyClass
{
public MyCollection Coll { get; private set; }
public MyClass() { this.Coll = new MyCollection(); }
}
class MyCollection : ...
0
votes
2
answers
191
views
System.Array does not support Add(), so how does collection initializer work?
In C# you can initialize an array like so:
var example = new int[] { 1, 4, 3 };
As quoted in Custom Collection Initializers:
The collection object to which a collection initializer is applied must ...
2
votes
1
answer
342
views
Asynchronous population of collection initializer
I would like to populate a collection using collection initializer that will call async methods:
public class Diagnostics
{
public async Task<IEnumerable<DiagnosticsInfo>> Get() => ...
5
votes
1
answer
131
views
How to implement a clean Custom Object Initializer for a Matrix class
I have a custom Matrix class that I would like to implement a custom object initializer similar to what a double[,] can use but can not seem to figure out how to implement it.
Ideally I would like ...
1
vote
0
answers
171
views
Will EF7 support collection initializers?
C# 6 introduced some cool additions to how objects and collections are initialized.
For example this code is valid in C#6:
void Main()
{
var ints = new[] { 1, 2, 3, 4, 5 };
var myClass = new ...
21
votes
4
answers
7k
views
C# 6.0's new Dictionary Initializer - Clarification [duplicate]
I've read that :
The team have generally been busy implementing other variations on
initializers. For example you can now initialize a Dictionary object
But looking at :
var Dic = new ...
2
votes
0
answers
36
views
Collection initializer and TypeInitializationException vs IEnumerable [duplicate]
I have used collection initializer for a Dictionary and received a TypeInitializationException:
public static Dictionary<Environment.SpecialFolder, string> specialFolders = new Dictionary<...
2
votes
1
answer
568
views
How to call a method with the C# collection initializer?
Case
This morning I refactored some Logging method and needed to change a method's 'params' parameter in a normal array. Consequently, the call to the method had to change with an array parameter. I'...
7
votes
1
answer
7k
views
Using TextBoxFor to set HTML attributes that have a namespace prefix
I am converting some existing HTML to work with ASP.NET MVC, and have some forms containing input fields that have additional attributes (that, for now, I need to preserve) that contain a namespace ...
6
votes
2
answers
322
views
Can another thread see partially created collection when using collection initializer?
Imagine this C# code in some method:
SomeClass.SomeGlobalStaticDictionary = new Dictionary<int, string>()
{
{0, "value"},
};
Let's say no one is using any explicit memory barriers or ...
4
votes
1
answer
2k
views
Object Initialization Not Working for Me
What am I missing here? I expected the following to work just fine:
public class ProposalFileInfo
{
public int FileId { get; set; }
public bool IsSupportDocument { get; set; }
}
// ...
var ...
11
votes
5
answers
11k
views
Using collection initializer syntax on custom types?
I have a large static list which is basically a lookup table, so I initialise the table in code.
private class MyClass
{
private class LookupItem
{
public int Param1 { get; set; }
...
14
votes
2
answers
429
views
Why are collection initializers on re-assignments not allowed?
I always thought it worked fine both ways. Then did this test and realized it's not allowed on re-assignments:
int[] a = {0, 2, 4, 6, 8};
works fine but not:
int [ ] a;
a = { 0, 2, 4, 6, 8 };
Any ...
4
votes
3
answers
178
views
Creating a New Class with Collection Initializers in C# [duplicate]
I have a class that I created. I would like to allow the class to be able to have a collection initializer. Here is an example of the class:
public class Cat
{
private Dictionary catNameAndType = ...