2012-05-22 100 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

希望幫助