C# 扩展数组类型以重载运算符
我想创建我自己的类扩展整数数组。这可能吗?我需要的是整数数组,可以通过“+”运算符将其添加到另一个数组(每个元素添加到每个数组),并通过“==”进行比较,因此它可以(希望)用作字典中的键。
问题是我不想在新类中实现整个 IList 接口,而只想将这两个运算符添加到现有数组类中。
我正在尝试做这样的事情:
class MyArray : Array<int>
但显然它不是这样工作的;)。
抱歉,如果我不清楚,但我现在正在寻找解决方案几个小时......
更新:
我尝试了这样的事情:
class Zmienne : IEquatable<Zmienne>
{
public int[] x;
public Zmienne(int ilosc)
{
x = new int[ilosc];
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
return base.Equals((Zmienne)obj);
}
public bool Equals(Zmienne drugie)
{
if (x.Length != drugie.x.Length)
return false;
else
{
for (int i = 0; i < x.Length; i++)
{
if (x[i] != drugie.x[i])
return false;
}
}
return true;
}
public override int GetHashCode()
{
int hash = x[0].GetHashCode();
for (int i = 1; i < x.Length; i++)
hash = hash ^ x[i].GetHashCode();
return hash;
}
}
然后像这样使用它:
Zmienne tab1 = new Zmienne(2);
Zmienne tab2 = new Zmienne(2);
tab1.x[0] = 1;
tab1.x[1] = 1;
tab2.x[0] = 1;
tab2.x[1] = 1;
if (tab1 == tab2)
Console.WriteLine("Works!");
并且没有效果。不幸的是,我不擅长接口和重写方法:(。至于我尝试这样做的原因。我有一些方程式,例如:
x1 + x2 = 0.45
x1 + x4 = 0.2
x2 + x4 = 0.11
还有更多,例如,我需要将第一个方程添加到第二个方程并搜索所有其他方程以找出是否有任何与导致该添加的 x'es 组合相匹配的方程。
也许我的方向完全错误?
I'd like to create my own class extending array of ints. Is that possible? What I need is array of ints that can be added by "+" operator to another array (each element added to each), and compared by "==", so it could (hopefully) be used as a key in dictionary.
The thing is I don't want to implement whole IList interface to my new class, but only add those two operators to existing array class.
I'm trying to do something like this:
class MyArray : Array<int>
But it's not working that way obviously ;).
Sorry if I'm unclear but I'm searching solution for hours now...
UPDATE:
I tried something like this:
class Zmienne : IEquatable<Zmienne>
{
public int[] x;
public Zmienne(int ilosc)
{
x = new int[ilosc];
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
return base.Equals((Zmienne)obj);
}
public bool Equals(Zmienne drugie)
{
if (x.Length != drugie.x.Length)
return false;
else
{
for (int i = 0; i < x.Length; i++)
{
if (x[i] != drugie.x[i])
return false;
}
}
return true;
}
public override int GetHashCode()
{
int hash = x[0].GetHashCode();
for (int i = 1; i < x.Length; i++)
hash = hash ^ x[i].GetHashCode();
return hash;
}
}
Then use it like this:
Zmienne tab1 = new Zmienne(2);
Zmienne tab2 = new Zmienne(2);
tab1.x[0] = 1;
tab1.x[1] = 1;
tab2.x[0] = 1;
tab2.x[1] = 1;
if (tab1 == tab2)
Console.WriteLine("Works!");
And no effect. I'm not good with interfaces and overriding methods unfortunately :(. As for reason I'm trying to do it. I have some equations like:
x1 + x2 = 0.45
x1 + x4 = 0.2
x2 + x4 = 0.11
There are a lot more of them, and I need to for example add first equation to second and search all others to find out if there is any that matches the combination of x'es resulting in that adding.
Maybe I'm going in totally wrong direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于单一类型,封装起来非常容易,如下所示。请注意,作为键,您也希望使其不可变。如果你想使用泛型,它会变得更难(要求更多信息):
For a single type, it is pretty easy to encapsulate, as below. Note that as a key you want to make it immutable too. If you want to use generics, it gets harder (ask for more info):
不允许扩展数组类,请参阅参考:http:// msdn.microsoft.com/en-us/library/system.array.aspx
您可以实现 IList(它具有基本方法),或者在您的类中封装一个数组并提供转换运算符。
如果您需要更多详细信息,请告诉我。
Its not permitted to extend the array class, see the reference: http://msdn.microsoft.com/en-us/library/system.array.aspx
You could either implement IList (which has the basic methods), or encapsulate an Array in your class and provide conversion operators.
Please let me know if you need more detail.
不能只使用List类吗?这已经通过 AddRange 方法完成了您想要的操作。
Can you not just use the List class? This already does what you want via the AddRange method.
实现 ienumerable 接口
implement the ienumerable interface