为什么编译器会发生这种情况 [英] Why does the compiler let this happen

查看:64
本文介绍了为什么编译器会发生这种情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么编译器不会在这样的代码块上抛出错误:


public string Email

{

get

{

返回电子邮件;

}

}

我无法想象任何时候递归会在一个属性中有用

getter而且这是我不可避免地输入的东西。

Why doesn''t the compiler throw an error on a block of code like this:

public string Email
{
get
{
return Email;
}
}
I can''t think of any time where recursion would be useful in a property
getter and it is something that I have accidently typed more than once.

推荐答案

我想不排除你*可能*有某种奇怪的代码,你设置一些变量,然后希望递归地

调用该属性。一个属性get,实际上只是一个方法包装器,在

之后,所以可以想象。


现在,实际上,没有人真正需要一个递归属性,

,因为它通常只是检索私有存储的值。但是,

编译器没有做出任何假设。


" Scott Simons" < Scott.Simons.At.MealMagic.Com.Remove.This>在留言中写道

新闻:D2 ********************************** @ microsof t.com ...
I guess it doesn''t rule out the fact that you *could* have some sort of
weird code where you are setting some variable and then wish to recursively
call the property. A property get, is really just a method wrapper, after
all, so it is conceivable.

Now, in practical terms, no one should really need a recursive property,
since typically it just retrieves a value stored privately. However, the
compiler doesn''t make any assumptions.

"Scott Simons" <Scott.Simons.At.MealMagic.Com.Remove.This> wrote in message
news:D2**********************************@microsof t.com...
为什么编译器不会在这样的代码块上抛出错误:

public string Email
{
获取
{
返回电子邮件;
}
}

我无法想到任何时候递归在属性中有用
getter,这是我不经意间输入的东西。
Why doesn''t the compiler throw an error on a block of code like this:

public string Email
{
get
{
return Email;
}
}
I can''t think of any time where recursion would be useful in a property
getter and it is something that I have accidently typed more than once.



>公共字符串电子邮件
> public string Email
{
获取
{
返回电子邮件;
}
}

我可以'想想任何时候递归在一个属性
getter中是有用的,这是我不经意地输入的东西。
{
get
{
return Email;
}
}
I can''t think of any time where recursion would be useful in a property
getter and it is something that I have accidently typed more than once.




编译器允许这样做,因为对于物业获取者而言,它是完全合法的。

返回自己。你是对的,在一个吸气剂中很少使用递归,

但想象一下,如果编译器在你需要这个时不允许这个

会有多么令人厌恶可能性。


Anders Nor?s http://dotnetjunkies.com/weblog/anoras/



The compiler allows this because it is perfectly legal for a property getter
to return itself. You''re right that recursion is seldom used in a getter,
but imagine how irretating it would be if the compiler didn''t allow this
when you needed this possibility.

Anders Nor?s
http://dotnetjunkies.com/weblog/anoras/


由于获取和设置是一个循环,因此在循环中更好地处理和奇怪的代码

没有参数化?


" Marina"写道:
Wouldn''t and weird code be better handled in a loop since gets and sets are
not parameterized?

"Marina" wrote:
我想它并不排除你*可能*有某种奇怪的代码,你设置一些变量,然后希望递归地打电话给财产。

之后,属性get,实际上只是一个方法包装器。

现在,实际上,没有人真正需要递归属性,
因为它通常只是检索私下存储的值。但是,
编译器没有做出任何假设。

Scott Simons < Scott.Simons.At.MealMagic.Com.Remove.This>在消息中写道
新闻:D2 ********************************** @ microsof t.com。 ..
I guess it doesn''t rule out the fact that you *could* have some sort of
weird code where you are setting some variable and then wish to recursively
call the property. A property get, is really just a method wrapper, after
all, so it is conceivable.

Now, in practical terms, no one should really need a recursive property,
since typically it just retrieves a value stored privately. However, the
compiler doesn''t make any assumptions.

"Scott Simons" <Scott.Simons.At.MealMagic.Com.Remove.This> wrote in message
news:D2**********************************@microsof t.com...
为什么编译器不会在这样的代码块上抛出错误:

public string Email
{
get
{
返回电子邮件;
}
}

我无法想到任何时候递归会对房产有用
getter,这是我意外输入的内容。
Why doesn''t the compiler throw an error on a block of code like this:

public string Email
{
get
{
return Email;
}
}
I can''t think of any time where recursion would be useful in a property
getter and it is something that I have accidently typed more than once.




这篇关于为什么编译器会发生这种情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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