C # Extend array type for operator overloading

I would like to create my own extending class of int classes. Is it possible? I need an int array, which can be added by the "+" operator to another array (each element is added to each) and compared with "==", so it can (hopefully) be used as a key in the dictionary.

The fact is that I do not want to implement the entire IList interface for my new class, but only add these two statements to the existing array class.

I am trying to do something like this:

class MyArray : Array<int> 

But it does not work that way, obviously;).

Sorry if I'm unclear, but I'm looking for a solution in a few hours ...

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 as follows:

 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. Unfortunately, I am not very well versed in interfaces and overriding methods :( Regarding the reason, I am trying to do this. I have some equations like:

x1 + x2 = 0.45
x1 + x4 = 0.2
x2 + x4 = 0.11

There are many more, and I need, for example, to add the first equation to the second and find all the others to find out if there is something that matches the combination of x'es, which leads to the addition.

Maybe I'll go in the completely wrong direction?

+4
source share
4 answers

For one type, it is quite easy to encapsulate it, as shown below. Note that as a key you want to make it immutable. If you want to use generics, it gets harder (ask for more information):

 using System; using System.Collections; using System.Collections.Generic; using System.Text; static class Program { static void Main() { MyVector x = new MyVector(1, 2, 3), y = new MyVector(1, 2, 3), z = new MyVector(4,5,6); Console.WriteLine(x == y); // true Console.WriteLine(x == z); // false Console.WriteLine(object.Equals(x, y)); // true Console.WriteLine(object.Equals(x, z)); // false var comparer = EqualityComparer<MyVector>.Default; Console.WriteLine(comparer.GetHashCode(x)); // should match y Console.WriteLine(comparer.GetHashCode(y)); // should match x Console.WriteLine(comparer.GetHashCode(z)); // *probably* different Console.WriteLine(comparer.Equals(x,y)); // true Console.WriteLine(comparer.Equals(x,z)); // false MyVector sum = x + z; Console.WriteLine(sum); } } public sealed class MyVector : IEquatable<MyVector>, IEnumerable<int> { private readonly int[] data; public int this[int index] { get { return data[index]; } } public MyVector(params int[] data) { if (data == null) throw new ArgumentNullException("data"); this.data = (int[])data.Clone(); } private int? hash; public override int GetHashCode() { if (hash == null) { int result = 13; for (int i = 0; i < data.Length; i++) { result = (result * 7) + data[i]; } hash = result; } return hash.GetValueOrDefault(); } public int Length { get { return data.Length; } } public IEnumerator<int> GetEnumerator() { for (int i = 0; i < data.Length; i++) { yield return data[i]; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public override bool Equals(object obj) { return this == (obj as MyVector); } public bool Equals(MyVector obj) { return this == obj; } public override string ToString() { StringBuilder sb = new StringBuilder("["); if (data.Length > 0) sb.Append(data[0]); for (int i = 1; i < data.Length; i++) { sb.Append(',').Append(data[i]); } sb.Append(']'); return sb.ToString(); } public static bool operator ==(MyVector x, MyVector y) { if(ReferenceEquals(x,y)) return true; if(ReferenceEquals(x,null) || ReferenceEquals(y,null)) return false; if (x.hash.HasValue && y.hash.HasValue && // exploit known different hash x.hash.GetValueOrDefault() != y.hash.GetValueOrDefault()) return false; int[] xdata = x.data, ydata = y.data; if(xdata.Length != ydata.Length) return false; for(int i = 0 ; i < xdata.Length ; i++) { if(xdata[i] != ydata[i]) return false; } return true; } public static bool operator != (MyVector x, MyVector y) { return !(x==y); } public static MyVector operator +(MyVector x, MyVector y) { if(x==null || y == null) throw new ArgumentNullException(); int[] xdata = x.data, ydata = y.data; if(xdata.Length != ydata.Length) throw new InvalidOperationException("Length mismatch"); int[] result = new int[xdata.Length]; for(int i = 0 ; i < xdata.Length ; i++) { result[i] = xdata[i] + ydata[i]; } return new MyVector(result); } } 
+4
source

It is not allowed to extend an array class, see link: http://msdn.microsoft.com/en-us/library/system.array.aspx

You can either implement IList (which has basic methods), or encapsulate Array in your class and provide conversion operators.

Please let me know if you need more details.

+5
source

Can you use the List class? This already does what you want with the AddRange method.

0
source

implement ienumerable interface

0
source

All Articles