[英]C# variable names with special character
I want to include some special characters in a string
variable name in C#.我想在 C# 中的
string
变量名称中包含一些特殊字符。
Example: string foo-bar = String.Empty;示例:字符串 foo-bar = String.Empty;
As far as my understand I can't declare a variable as I mentioned in the above example.据我所知,我不能像上面的例子中提到的那样声明一个变量。 Is there any way around to declare a variable name with "-" included?
有什么办法可以用“-”来声明一个变量名?
You can't just choose any sequence of characters as a variable name.
您不能只选择任何字符序列作为变量名称。 This isn't as worrying as it might sound, however, because you're still left with a very flexible naming system.
然而,这并不像听起来那么令人担忧,因为您仍然拥有一个非常灵活的命名系统。 The basic variable naming rules are as follows:
基本的变量命名规则如下:
The first character of a variable name must be either a letter, an underscore character (_), or the at symbol (@).
变量名的第一个字符必须是字母、下划线字符 (_) 或 at 符号 (@)。 Subsequent characters may be letters, underscore characters, or numbers.
后续字符可以是字母、下划线字符或数字。
No, this is not possible to do in C#.不,这在 C# 中是不可能的。
If you really, really, really want to so this, you could use a Dictionary<string, string>
:如果你真的,真的,真的想要这样做,你可以使用
Dictionary<string, string>
:
Dictionary<string, string> someVars = new Dictionary<string, string>()
{
{"foo-bar", String.Empty},
{"bar-foo", "bazinga"}
}
Using them would look like this:使用它们看起来像这样:
string newstring = someVars["foo-bar"] + "Hello World!";
Instead of just using the variable name, you would look up the string in your dictionary.您不仅可以使用变量名,还可以在字典中查找字符串。 Note that this is very inefficient and just intended as a joke, so please do no really use this ;)
请注意,这是非常低效的,只是一个玩笑,所以请不要真正使用它;)
If you are trying to deserialize object, you can use JsonProperty to acieve this.如果您试图反序列化对象,您可以使用 JsonProperty 来实现这一点。
Example:例子:
public class SubscriptionListJsonData
{
[JsonProperty(PropertyName = "subscriptions")]
public string SubscriptionData { get; set; }
[JsonProperty(PropertyName = "@nextLink")]
public string nextLink { get; set; }
}
Follow this link for partially reading Json. 按照此链接部分阅读 Json。
你不能在 c# 和大多数其他编程语言中做到这一点。我还建议你遵循 C# 命名约定,因为它帮助你以一种 - 至少对我来说 - 总是感觉很舒服的方式阅读你的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.