??基本的C#语言问题? [英] ?? Basic C# Language Questions ??

查看:62
本文介绍了??基本的C#语言问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有两个问题我希望有人能就C#回答。


首先,我发现C#没有允许本地只读变量。例如,

以下是非法的:


void X(){

readonly int i = 1;

}


我知道对于这个简单的例子我可以使用const但是当你必须使用readonly时有

次。为什么不能只读变量是本地的?


我的第二个问题与锯齿状数组有关。以下构造是

legal:

int [] a1 = {1,2,3};


但这是不:

int [] [] a2 = {{1,2,3},{4,5}};


a2的上述声明将生成两个错误(两者都相同):

数组初始值设定项只能用于变量或字段初始值设定项。尝试使用新的表达式来代替



我在这里显然遗漏了一些东西。对于我的(显然不正确的)

思考的方式,a2 * IS *正在使用变量初始化程序进行初始化。


如果我将上述声明更改为:


int [] [] a2 = {new int [] {1,2,3},new int [] {4,5}};


错误消失了。有人能澄清这种行为吗,

拜托?


非常感谢。

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.

推荐答案

首先,我没有看到为什么任何外部代码永远不会访问的局部变量应该是只读的原因。你可以使用

常量(相当于readonly)。但是readonly真正意味着什么是外部代码(例如,如果你正在创建一个DLL库而某人

else应该使用它)不能改变这个属性,虽然你可以

仍然能够改变内部变量。它仅适用于暴露于外部代码的

属性。


接下来,对于任何数组,正确属性。声明是:

int [] a = new int [] {xxxxxxx];

但是C#通过使用不太合法而更容易版本

int [] a = {xxxxxxx};

这使它成为简写符号。

但它并不适用于锯齿状阵列的内括号。

你说的非法符号对多维

阵列有效。


Freiddie
http://fei.yuanbw.googlepages.com/
http://freiddy.blogspot.com/
< a rel =nofollowhref =http://scilearn.blogspot.com/target =_ blank> http://scilearn.blogspot.com/

For one thing, I don''t see any reason why local variables, which are
never accessed by any external code, should be readonly. You can use
constants (equivalent of readonly). But what readonly really means is
that external code (e.g. if you''re making a DLL library and someone
else is supposed to use it) cannot change this property, while you may
still be able to change the inner variable. It only applies to
properties, which are exposed to external codes.

Next, for any array the "proper" declaration is:
int[] a = new int[] { xxxxxxx ];
But C# makes it easier, by using the "less legal" version
int[] a = { xxxxxxx };
That makes it a shorthand notation.
But it doesn''t apply to the inner brackets of the jagged arrays.
The notation you said to be illegal is valid for multidimensional
arrays, though.

Freiddie
http://fei.yuanbw.googlepages.com/
http://freiddy.blogspot.com/
http://scilearn.blogspot.com/


''readonly''变量可以在声明中或在
构造函数中初始化,但不能在其他任何地方初始化。您是如何期望这与本地

变量有关?


在您的数组示例中,您的第一个初始化程序是一个二维数组

初始化程序。你的第二次尝试适合于锯齿状阵列,因为它是一个数组阵列。

-

David Anton
< a rel =nofollowhref =http://www.tangiblesoftwaresolutions.comtarget =_ blank> www.tangiblesoftwaresolutions.com

即时C#:VB转换为C#转换器

即时VB:C#到VB转换器

C ++到C#转换器:将C ++转换为C#

即时C ++:将C#或VB转换为C ++ / CLI

" Robin Bowning"写道:
''readonly'' variables can be initialized either in the declaration or in a
constructor, but nowhere else. How did you expect this to relate to local
variables?

In your array example, your first initializer is a 2-dimensional array
initializer. Your second try is appropriate for a jagged array since it is
an array of arrays.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
C++ to C# Converter: converts C++ to C#
Instant C++: converts C# or VB to C++/CLI
"Robin Bowning" wrote:

大家好,


我有两个问题我希望有人可以回答一下C#。


首先,我注意到C#不允许本地只读变量。例如,

以下是非法的:


void X(){

readonly int i = 1;

}


我知道对于这个简单的例子我可以使用const但是当你必须使用readonly时有

次。为什么不能只读变量是本地的?


我的第二个问题与锯齿状数组有关。以下构造是

legal:

int [] a1 = {1,2,3};


但这是不:

int [] [] a2 = {{1,2,3},{4,5}};


a2的上述声明将生成两个错误(两者都相同):

数组初始值设定项只能用于变量或字段初始值设定项。尝试使用新的表达式来代替



我在这里显然遗漏了一些东西。对于我的(显然不正确的)

思考的方式,a2 * IS *正在使用变量初始化程序进行初始化。


如果我将上述声明更改为:


int [] [] a2 = {new int [] {1,2,3},new int [] {4,5}};


错误消失了。有人能够澄清这种行为吗,

拜托?


非常感谢。
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.

下面......

" Freiddie" < fe ******** @ gmail.com写信息

新闻:11 ********************* @ l77g2000hsb.googlegro ups.com ...
Below...
"Freiddie" <fe********@gmail.comwrote in message
news:11*********************@l77g2000hsb.googlegro ups.com...

首先,我没有看到任何原因导致局部变量,这是

永远不会被任何外部代码访问,应该是readonly。你可以使用

常量(相当于readonly)。
For one thing, I don''t see any reason why local variables, which are
never accessed by any external code, should be readonly. You can use
constants (equivalent of readonly).



const肯定不等于readonly。当然,他们是相似的,在有限的范围内,但并非完全相同。尝试这样做:

const MyClass mc = new MyClass();

const is certainly not the equivalent of readonly. Sure, they''re similar, to
a limited extent, but not at all the same. Try doing this:
const MyClass mc = new MyClass();


但是readonly的真正含义是

外部代码(例如,如果你正在创建一个DLL库而某人

else应该使用它)不能改变这个属性,而你可以

仍然可以更改内部变量。它仅适用于

属性,这些属性暴露给外部代码。
But what readonly really means is
that external code (e.g. if you''re making a DLL library and someone
else is supposed to use it) cannot change this property, while you may
still be able to change the inner variable. It only applies to
properties, which are exposed to external codes.



" ... readonly真正含义..."?不 - 你已经描述了''readonly''可能使用的'b $ b'。 readonly的意思是,一旦赋值,

就不能重新分配。这就是它的全部意味着。你已经描述了使用''readonly''的一个

可能的意图,但肯定不是唯一的原因。
关于*的细节没有任何意义为什么*我想做一个本地的

readonly。似乎没有充分的理由说明为什么我不能给b
.


"...what readonly really means..."? No -- you have described one possible
use of ''readonly''. What readonly means is that once a value is assigned it
cannot be re-assigned. That''s all it "means". You have described one
possible intention of using ''readonly'', but certainly not the only reason.
There''s no sense in going into details as to *why* I want to make a local
readonly. It just seems that there''s no good reason why I shouldn''t be able
to.



接下来,对于任何阵列,适当阵列声明是:

int [] a = new int [] {xxxxxxx];

但是C#通过使用不太合法而更容易版本

int [] a = {xxxxxxx};
Next, for any array the "proper" declaration is:
int[] a = new int[] { xxxxxxx ];
But C# makes it easier, by using the "less legal" version
int[] a = { xxxxxxx };



少合法?什么都没有不太合法关于它。

"Less legal"? There''s nothing "less legal" about it.


这使它成为简写符号。

但它不适用于内部括号参差不齐的数组。

你说的非法符号对多维

数组有效。
That makes it a shorthand notation.
But it doesn''t apply to the inner brackets of the jagged arrays.
The notation you said to be illegal is valid for multidimensional
arrays, though.



是的,谢谢。我自己发现了这个。这就是我的要求。

你基本上重新措辞了我已经说过的话。

Yes, Thank you. I''ve discovered this myself. That''s what I was asking about.
You basically re-phrased what I''d already stated.


这篇关于??基本的C#语言问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆