I have following code definig an array
public class PalphabetsDic
{
public static string[] PAlphCodes = new string[3] {
PAlphCodes[0] = "1593",
PAlphCodes[1] = "1604",
PAlphCodes[2] = "1740",
};
}
When I use this array
var text = PalphabetsDic.PAlphCodes[1]
Gives error:
The type initializer for 'Dota2RTL.PalphabetsDic' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object.
Please can someone help me on this?
Note that What is a NullReferenceException, and how do I fix it? covers arrays, but PAlphCodes = new string[3]
should be setting it up to be not null
.
When initializing the way you are you don't need to index the values:
public static string[] PAlphCodes = new string[] {
"1593",
"1604",
"1740",
};
To expand upon what Kennedy answered -- you can also use
public static string[] PAlphCodes = { "1593", "1604", "1740" };
The reference manual has a listing of all the possible ways -- but the one Kennedy suggested -- and this method -- are probably the most common.
https://msdn.microsoft.com/en-us/library/aa287601(v=vs.71).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With