I've got a function which operates on pixels. I want to create one list with RGB values, but when I declare it this way:
我有一个对像素进行操作的功能。我想用RGB值创建一个列表,但是当我以这种方式声明时:
List<int[]> maskPixels = new List<int[3]>();
It gives me error:
它给了我错误:
Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
无法在变量声明中指定数组大小(尝试使用'new'表达式初始化)
Adding pixels is done like this: maskPixels.Add(new int[] { Red, Green, Blue });
添加像素的方法如下:maskPixels.Add(new int [] {Red,Green,Blue});
Is there a way to do this, or I have to use new List<int[]>();
instead?
有没有办法做到这一点,或者我必须使用新的List
5 个解决方案
#1
5
That's not possible. Think about it for just a second. When you have a generic type, say List<T>
, T
is a type parameter and for specific instances of the generic type, you fill in a type T
.
那是不可能的。想想它只是一秒钟。当你有一个泛型类型,比如List
But int[3]
is not a type! Precisely, it's an array-creation-expression
in the C# grammar.
但是int [3]不是一个类型!确切地说,它是C#语法中的数组创建表达式。
If you want to limit to a type that can only hold three values, may I suggest as a first cut Tuple<int, int, int>
? But even better, I recommend a type dedicated to representing RGB like System.Drawing.Color
(use Color.FromArgb(int, int, int)
) or your own custom type if necessary. The reason I would lean towards the latter is because not all Tuple<int, int, int>
are valid representations of RGB!
如果你想限制一个只能容纳三个值的类型,我可以建议作为第一个切割Tuple
#2
7
There is no way to do something similar, but since you are using this for RGB values, why don't you use Color
class instead?
没有办法做类似的事情,但由于你将它用于RGB值,为什么不使用Color类呢?
List<Color> maskPixels = new List<Color>();
And initialize each Color like this:
并像这样初始化每个颜色:
Color c = Color.FromArgb(R,G,B); //assuming R,G,B are int values
If your values are in the range of 0-255 this is the most natural way of storing them. You have predefined getters and setters in order to obtain each color component.
如果您的值在0-255范围内,这是存储它们的最自然方式。您有预定义的getter和setter以获取每个颜色组件。
#3
5
You can't do array initialization like this, and further each int[]
could technically be a different size. How about a different data structure, List<Tuple<int, int, int>>
?
你不能像这样进行数组初始化,而且每个int []在技术上可以是不同的大小。如何使用不同的数据结构List
This would allow you to strictly have three integer values in the list, and it's searchable via LINQ faster than an array because it's a well defined structure with properties.
这将允许您在列表中严格地具有三个整数值,并且它可以通过LINQ比数组更快地搜索,因为它是具有属性的良好定义的结构。
#4
5
I'd suggest having your own value-type for this:
我建议为此拥有自己的值类型:
public struct Rgb
{
int R,
int G,
int B
}
List<Rgb> list = new List<Rgb>;
#5
-1
[Flags]
public enum RGB
{
R,
G,
B
}
...
...
public List<RGB> l = new List<RGB>();
l.Add(RGB.r | RGB.G)
#1
5
That's not possible. Think about it for just a second. When you have a generic type, say List<T>
, T
is a type parameter and for specific instances of the generic type, you fill in a type T
.
那是不可能的。想想它只是一秒钟。当你有一个泛型类型,比如List
But int[3]
is not a type! Precisely, it's an array-creation-expression
in the C# grammar.
但是int [3]不是一个类型!确切地说,它是C#语法中的数组创建表达式。
If you want to limit to a type that can only hold three values, may I suggest as a first cut Tuple<int, int, int>
? But even better, I recommend a type dedicated to representing RGB like System.Drawing.Color
(use Color.FromArgb(int, int, int)
) or your own custom type if necessary. The reason I would lean towards the latter is because not all Tuple<int, int, int>
are valid representations of RGB!
如果你想限制一个只能容纳三个值的类型,我可以建议作为第一个切割Tuple
#2
7
There is no way to do something similar, but since you are using this for RGB values, why don't you use Color
class instead?
没有办法做类似的事情,但由于你将它用于RGB值,为什么不使用Color类呢?
List<Color> maskPixels = new List<Color>();
And initialize each Color like this:
并像这样初始化每个颜色:
Color c = Color.FromArgb(R,G,B); //assuming R,G,B are int values
If your values are in the range of 0-255 this is the most natural way of storing them. You have predefined getters and setters in order to obtain each color component.
如果您的值在0-255范围内,这是存储它们的最自然方式。您有预定义的getter和setter以获取每个颜色组件。
#3
5
You can't do array initialization like this, and further each int[]
could technically be a different size. How about a different data structure, List<Tuple<int, int, int>>
?
你不能像这样进行数组初始化,而且每个int []在技术上可以是不同的大小。如何使用不同的数据结构List
This would allow you to strictly have three integer values in the list, and it's searchable via LINQ faster than an array because it's a well defined structure with properties.
这将允许您在列表中严格地具有三个整数值,并且它可以通过LINQ比数组更快地搜索,因为它是具有属性的良好定义的结构。
#4
5
I'd suggest having your own value-type for this:
我建议为此拥有自己的值类型:
public struct Rgb
{
int R,
int G,
int B
}
List<Rgb> list = new List<Rgb>;
#5
-1
[Flags]
public enum RGB
{
R,
G,
B
}
...
...
public List<RGB> l = new List<RGB>();
l.Add(RGB.r | RGB.G)