override default new operator for an array of a class C# -
i using pinvokes call native code.
if want create array of native objects following
public class myclass() { // allocate single myclass; public myclass() { _myclass = myclass_create(); _length = 1; } public myclass(int numargs) { //pinvoke call create array of myclass; _myclass = myclass_array_create(uintptr); _length = numargs; } //access indexed element of myclass public myclass this[int index] { { myclass ret = new myclass(); ret._myclass = myclass_array_element(this._myclass, (uintptr)index); return ret; } } public int length { { return _length; } } public void foo(){ //lots of other code } [dllimport(dll_import_target)] private static extern intptr myclass_create(); [dllimport(dll_import_target)] private static extern intptr myclass_array_create(uintptr numargs); [dllimport(dll_import_target)] private static extern intptr myclass_array_element(intptr args, uintptr index); // ... more dllimports here ... //pointer native object intptr _myclass; int _length; }
this used follows:
// create array of 15 myclass objects myclass myclass = new myclass(15); for( int = 0; < myclass.length; ++i) { //run foo on each object in array myclass[i].foo() }
i have working find little unusual call new array out doing array new.
is there way can override new operator class typical use of new operator can used instead?
i code this
// create array of 15 myclass objects myclass[] myclass = new myclass[15]; for( int = 0; < myclass.length; ++i) { //run foo on each object in array myclass[i].foo() }
is there way code?
no, cannot in way override behavior of how array initialized. need either:
use different type wraps array, showed in question.
call method on array after creating add additional initialization (i.e. assign bunch of values each index).
create new method that, when called, create , initialize array.