I “grew up” on Java and have recently made a full switch to C#. I am just teaching myself ATM, and just going back, redoing all my old programming assignments that were in Java using C# now. Here is a particular line of code where I am trying to use generics and instantiate the stack array.
stack = (T[])(new object[def_cap]);
which gives me this compiler error
Cannot convert type 'object[]' to 'T[]' (CS0030)
Cannot implicitly convert type 'object[]' to 'T[]' (CS0029)
I guess the cast operator works differently in C# than in Java, and was wondering if anyone could enlighten me. Thank you!
Just use
stack = new T[def_cap];instead. You already have the type and are able to use it directly.