我如何使用C#保留关键字作为属性名称不带@前缀? [英] How do I use a C# reserved keyword as a property name without the @ prefix?

查看:540
本文介绍了我如何使用C#保留关键字作为属性名称不带@前缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设计一个类,其中一个属性名称必须收益,但是当我创建一个属性名称,如收益然后我得到一个错误。

I need to design a class where one property name has to be return, but when I create a property name like return then I get an error.

经过一番研究,我发现了一个可以通过添加使用保留的关键字作为属性或变量名的 @ 前缀C#,或者通过在方括号 [] 在VB.NET括起来。例如:

After some research I found out that one can use a reserved keyword as a property or variable name by adding a @ prefix in C#, or by enclosing it in square brackets [] in VB.NET. For example:

var @class = new object();



因此​​,这里是我的课堂设计规范。

So here is my class design code.

public class Person
{
    string _retVal;

    public string @return
    {
        get { return _retVal; }
        set { _retVal = value; }
    }
}

...
Person p = new Person();
p.@return = "hello";

现在我没有得到任何错误,但是当我试图访问属性名称,如收益然后我需要写像 @返回,我不想要的名字。我想访问属性名称,如 p.return =你好; 而不是点@返回=你好; ,所以我想知道是否有办法做到这一点?

Now I am not getting any error, but when I try to access property name like return then I need to write the name like @return, which I don't want. I want to access the property name like p.return = "hello"; instead of p.@return = "hello"; so I'd like to know if there is any way to do that?

推荐答案

您不能。这是一个保留关键字。这办法你不能。 ,对比度为上下文关键字,这通常意味着我们增加了这个以后,所以我们需要它在某些预先存在的情况下工作,

You can't. It is a reserved keyword. That means "you can't". Contrast to "contextual keywords" which usually means "we added this later, so we needed it to work in some pre-existing scenarios".

下面适度的答案是: 。使用 @返回

The moderate answer here is: use @return.

在这里更好的回答是:重命名你的财产。也许返回值

A better answer here is: rename your property. Perhaps ReturnValue.

还有的,比方说,返回选项 - 但你可能需要考虑不区分大小写的语言太

There is also the option of, say, Return - but you might need to think about case-insensitive languages too.

这篇关于我如何使用C#保留关键字作为属性名称不带@前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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