开发者

Can array index be floating in C#.net?

开发者 https://www.devze.com 2023-03-19 19:38 出处:网络
I have array in C#, and my logic is stuck and it can only work if i can have floating indexes. Can day be 0.5 or 2.5 like that

I have array in C#, and my logic is stuck and it can only work if i can have floating indexes.

Can day be 0.5 or 2.5 like that

days[day, col]=1;

Please let me开发者_JAVA百科 know solution


You can write indexers using any type you like, e.g.

public double this[double x, int y]
{
    get { ... }
    set { ... }
}

You can't index into an array by a non-integer though.


Floats generally make terrible indexes, as it's not only possible but indeed quite common for two similar-looking floats to compare as unequal. (For instance, in most environments (1/3 and 1-(2/3) will give two distinct results.) Is there some reasonably small constant you can multiply your indices by to make them always integers?


You should prefer Indexers in this situation.

Use this keyword with your function and put these parameters.

EDIT: As Jon says; these are non-integer, you can't do it.


You can use Dictionary<> instead of array, where the key can be whatever you want.


Aren't you actually looking for indexing of arrays of arrays aka jagged arrays? Those are indexed like this: days[day][col]. More information here:

MSDN on Jagged Arrays

0

精彩评论

暂无评论...
验证码 换一张
取 消