8

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:

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)

Adding pixels is done like this: maskPixels.Add(new int[] { Red, Green, Blue });

Is there a way to do this, or I have to use new List<int[]>(); instead?

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
Thomas
  • 93
  • 1
  • 4
  • 4
    why don't you use List instead? – Paritosh Jun 10 '13 at 13:27
  • 1
    I'd recommend a `List` as well. But just to throw out an alternative: have you considered an `int[,]` or `int[,,]` (i.e. multi-dimensional array; the latter could be useful to store x,y,color)? Those, unlike jagged arrays (`int[][]`) or lists of arrays (`List`) are always rectangular/cuboid, i.e. you can't have varying numbers of elements. – Tim S. Jun 10 '13 at 13:35

5 Answers5

8

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?

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.

Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33
  • I would hope that they are in that range. There's not many (or indeed, none AFAIAA) Windows API calls that have RGB values greater than 255... – Matthew Watson Jun 10 '13 at 13:31
  • @MatthewWatson Offcourse, I was more thinking on some other proprietary color coding used for hardware or I don't know what other purpose. In case OP is storing them before converting to the 0-255 range. – Nikola Davidovic Jun 10 '13 at 13:33
  • It would require some more input from the OP, but I wonder if he's going to be doing some maths on these values, and perhaps `Color` is not amenable to do that - but then, who knows at this point, this could be the perfect solution. – Moo-Juice Jun 10 '13 at 13:34
  • @Moo-Juice Totally agree, that is why I added the disclaimer :-) – Nikola Davidovic Jun 10 '13 at 13:35
6

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.

But int[3] is not a type! Precisely, it's an array-creation-expression in the C# grammar.

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!

jason
  • 236,483
  • 35
  • 423
  • 525
  • I completely forgot about Color class. That's the thing I'll use. And great explanation about `array-creation-expression`; exactly what I was looking for. Thanks! – Thomas Jun 10 '13 at 14:18
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>>?

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.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
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>;
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
  • Don't you mean `List>` ? – Arion Jun 10 '13 at 13:29
  • 5
    @Arion, nope :) I (believe) the OP wants to store a list of RGB values, not a List of 3 RGB values. – Moo-Juice Jun 10 '13 at 13:30
  • Since I can't use `Tuple` (.NET 2.0), besides `Color` class, I think this is the best way to go. And I guess this is the only way (struct) for non-color values. – Thomas Jun 10 '13 at 14:36
-1
 [Flags]
    public enum RGB
    {
        R,
        G,
        B
    }

...

public List<RGB> l = new List<RGB>();
l.Add(RGB.r | RGB.G)
Yaugen Vlasau
  • 2,148
  • 1
  • 17
  • 38