2012-05-22 102 views
-1

我这样做从Java到C#C#:如何创建一个类在C#

在我的Java,我有这个类

public class Axe extends ByteArray { 

} 

通过右键我想应该是这样一个转换扩展的ByteArray,

public class Axe : ByteArray { 

} 

但问题是,在C#中不具备的ByteArray给我延长

谢谢

+0

你能解释一下你希望如何扩展类。添加这个上下文将帮助我们给你更好的答案。 –

+0

能否请你解释你需要什么?从你的帖子中不清楚。 ByteArray是C#中的一个字节[]。在.NET3 +你不能把它扩展这一点,但你可以写扩展方法:http://en.wikipedia.org/wiki/Extension_method – MichelZ

回答

1

不能扩展一个字节数组,但如果你想使用你的类像一个数组,你可以提供索引器:

public class Axe { 

    private byte[] data = new byte[whateverLength]; 

    public byte this[int index] { 
     get { return data[index]; } 
     set { data[index] = value; } 
    } 

} 

那么你可以做这样的事情:

Axe myAxe = new Axe(); 
myAxe[someIndex] = 5; 
1

你在找a:Byte []吗?

0

,如果你绝对要实现自己的字节类型集合实现诸如IList的接口之一:

public class Axe : IList<Byte> 

,或者如果你只是需要的比特而不是字节考虑使用BitArray

+0

你不能继承像'Array' /阵列。 (虽然你也可以继承'名单'它通常不是非常有用,因为'名单'有,你可以重写没有虚拟成员。) – LukeH

+0

感谢,指出与岗位相应修改 – mtijn

2

也许你应该只写一些扩展方法byte[]

static class ByteExtensions 
{ 
    public static string DoSomething(this byte[] x) 
    { 
     return "Length of this byte array: " + x.Length; 
    } 
} 

// ... 

void Foo() 
{ 
    var b = new byte[5]; 
    b.DoSomething(); 
} 
2

ByteArray是在Java包装类wrappes字节[](字节阵列)和提供方法来maipulate。如果需要,你可以写在C#你自己的包装类,

Link提供样品包装类在Java中ByteArray

希望帮助