Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何声明带有连字符的变量_C#_.net - Fatal编程技术网

C# 如何声明带有连字符的变量

C# 如何声明带有连字符的变量,c#,.net,C#,.net,最近,我在类中遇到了一个json对象名声明,并能够通过以下方法解决它: [JsonProperty("flv-270p")] public string flv270p { get; set; } 那么,如果我想在没有JsonProperty的情况下声明flv-270p,我该如何做呢 比如: public string flv270-p { get; set; } 简单回答:你不能 .NET支持任意成员名称,包括CIL语言中非法的成员名称。但是,如果语言不支持它,则不能使

最近,我在类中遇到了一个json对象名声明,并能够通过以下方法解决它:

  [JsonProperty("flv-270p")]
        public string flv270p { get; set; }
那么,如果我想在没有JsonProperty的情况下声明flv-270p,我该如何做呢

比如:

public string flv270-p { get; set; }

简单回答:你不能

.NET支持任意成员名称,包括CIL语言中非法的成员名称。但是,如果语言不支持它,则不能使用它,这包括连字符和破折号。因此,C#中的自动属性在其隐藏的备份字段名称中使用

C#允许您在关键字前面加上
@
符号,以允许您将其用作变量或成员名称(例如
public int@class
public void@struct(int@interface)
),但这是为了消费者的利益


但是,c#在任何情况下都不允许在名称中使用连字符,因为该字符用于一元求反和二元减法子运算符。名称
flv270-p
被解释为“
flv270
减去
p
”.

flv-270p
在C#中不是有效的变量名。每当您在代码中键入类似的内容时,编译器会认为您正在尝试执行“flv270减p”:)

您不能:

有效名称定义为:

identifier:
    available-identifier
    @   identifier-or-keyword
available-identifier:
    An identifier-or-keyword that is not a keyword
identifier-or-keyword:
    identifier-start-character   identifier-part-charactersopt
identifier-start-character:
    letter-character
    _ (the underscore character U+005F) 
identifier-part-characters:
    identifier-part-character
    identifier-part-characters   identifier-part-character
identifier-part-character:
    letter-character
    decimal-digit-character
    connecting-character
    combining-character
    formatting-character
letter-character:
    A Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nl
    A unicode-escape-sequence representing a character of classes Lu, Ll, Lt, Lm, Lo, or Nl
combining-character:
    A Unicode character of classes Mn or Mc
    A unicode-escape-sequence representing a character of classes Mn or Mc
decimal-digit-character:
    A Unicode character of the class Nd
    A unicode-escape-sequence representing a character of the class Nd
connecting-character:
    A Unicode character of the class Pc
    A unicode-escape-sequence representing a character of the class Pc
formatting-character:
    A Unicode character of the class Cf
    A unicode-escape-sequence representing a character of the class Cf 
连字符减号属于Pd类,因此在上面的代码中不允许使用

这确实是为什么在名称中允许Pd类的东西是个坏主意的原因之一:

int x-y = 3;
int x = 10;
int y = 2;
int z = x-y; //3 or 8? Impossible to tell if the first line were allowed.

其他答案很好地解释了为什么你不能按照你的要求进行测试。这就是说,我遇到过这样的情况:我希望我的对象元数据具有连字符,以便制定针对SEO优化的描述性URL。您在问题中使用的属性方法是有效的,但是您也可以使用约定来指示这种没有属性的自定义。一个建议是预处理成员名称,使用PascalCase或在单词之间插入连字符分隔符。这可能会导致太多元数据更改。另一种选择是使用下划线“#”,这是C#names中的法定字符,并在预处理中用连字符替换下划线。您的问题并没有充分说明您的用例如何在元数据上下文中实现/注入,但希望这些想法在您设计api时有所帮助。

实际上,您可以像这样使用代码

public string flv270_p { get; set; }
并返回一个重新格式化的类,该类将“-”替换为“\”

比如说,, 如果你有这个

var jsonstring = ApiRequest.DoRequest(ApiRequest.GetBaseUriFor(typeof(SMS), "/sms/json"), new Dictionary<string, string>()
        {
            {"from", request.from},
            {"to", request.to},
            {"text", request.text}
        });

        return JsonConvert.DeserializeObject<SMSResponse>(jsonstring);
var jsonstring=ApiRequest.DoRequest(ApiRequest.GetBaseUriFor(typeof(SMS),“/SMS/json”),新字典()
{
{“from”,request.from},
{“to”,request.to},
{“text”,request.text}
});
返回JsonConvert.DeserializeObject(jsonstring);
这样做,

var jsonstring = ApiRequest.DoRequest(ApiRequest.GetBaseUriFor(typeof(SMS), "/sms/json"), new Dictionary<string, string>()
        {
            {"from", request.from},
            {"to", request.to},
            {"text", request.text}
        }).Replace("-","_");

        return JsonConvert.DeserializeObject<SMSResponse>(jsonstring);
var jsonstring=ApiRequest.DoRequest(ApiRequest.GetBaseUriFor(typeof(SMS),“/SMS/json”),新字典()
{
{“from”,request.from},
{“to”,request.to},
{“text”,request.text}
}).替换(“-”、“”);
返回JsonConvert.DeserializeObject(jsonstring);

你已经这么做了,这到底是个什么问题?@Tigran-连字符在成员名称中是不合法的。@Tigran:我想问题是你如何声明它并让它工作。只要连字符兼作减号,它就永远不会工作。太含糊不清了。询问是否有其他方法用禁止的名称声明变量是错误的吗???为什么有这么多人要小心,不要用“\ux”替换一个合法的“-”。我确实这样做了,而且必须修复:)