R
Robin Bowning
Hi all,
I have two questions I was hoping someone could answer regarding C#.
First, I noticed C# does not allow local readonly variables. For example,
the following is illegal:
void X() {
readonly int i = 1;
}
I know that for this simple example I could have used const but there are
times when you must use readonly. Why can't readonly variables be local?
My second question has to do with jagged arrays. The following construct is
legal:
int[] a1 = {1, 2, 3};
but this is not:
int[][] a2 = {{1, 2, 3}, {4, 5}};
The above declaration of a2 will generate two errors (both the same):
"Array initializers can only be used in a variable or field initializer. Try
using a new expression instead."
I'm obviously missing something here. To my (obviously incorrect) way of
thinking, a2 *IS* being initialized with a variable initializer.
If I change the above declaration to this:
int[][] a2 = { new int[] {1, 2, 3}, new int[] {4, 5}};
the errors goes away. Would someone be able to clarify this behavior,
please?
Thanks very much.
I have two questions I was hoping someone could answer regarding C#.
First, I noticed C# does not allow local readonly variables. For example,
the following is illegal:
void X() {
readonly int i = 1;
}
I know that for this simple example I could have used const but there are
times when you must use readonly. Why can't readonly variables be local?
My second question has to do with jagged arrays. The following construct is
legal:
int[] a1 = {1, 2, 3};
but this is not:
int[][] a2 = {{1, 2, 3}, {4, 5}};
The above declaration of a2 will generate two errors (both the same):
"Array initializers can only be used in a variable or field initializer. Try
using a new expression instead."
I'm obviously missing something here. To my (obviously incorrect) way of
thinking, a2 *IS* being initialized with a variable initializer.
If I change the above declaration to this:
int[][] a2 = { new int[] {1, 2, 3}, new int[] {4, 5}};
the errors goes away. Would someone be able to clarify this behavior,
please?
Thanks very much.