6

I want to include some special characters in a string variable name in C#.

Example: string 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?

Sergey
  • 1,608
  • 1
  • 27
  • 40
kranthiv
  • 174
  • 1
  • 1
  • 8
  • 4
    Why do you need to include "-"? Interested to hear the reason. – Carbine Aug 09 '14 at 09:41
  • 1
    Do you need this for serialization? Use attributes. Is it for an HTML attribute in MVC? Use underscores. Explain why you need this. – CodeCaster Aug 09 '14 at 09:46
  • @CodeCaster I am trying to develop **sass** like css3 precompiler using c#. Many of the css attribute names include "-". So, I am declaring all the css attributes as properties which is involving "-". When i covert the .cs file to .css using reflections or Expression Tree, i will get exact css attribute names from the properties. – kranthiv Aug 09 '14 at 10:02
  • 1
    @kranthiv I think your problem asks for a Dictionary type. – Pieter21 Aug 09 '14 at 10:11

4 Answers4

10

From MSDN:

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 (@). Subsequent characters may be letters, underscore characters, or numbers.

Sergey
  • 1,608
  • 1
  • 27
  • 40
6

No, this is not possible to do in C#.


If you really, really, really want to so this, you could use a 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 ;)

Chris Jensen
  • 485
  • 3
  • 14
ThreeFx
  • 7,250
  • 1
  • 27
  • 51
2

If you are trying to deserialize object, you can use JsonProperty to acieve this.

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.

0

You can't do that in c# and most of other programming languages .. I also advice you to follow the C# naming conventions as it help you read your code in a way that -at least for me- always felt comfortable .

Ramy M. Mousa
  • 5,727
  • 3
  • 34
  • 45
  • 2
    F# is nice and flexible with variable naming, even permits spaces, there's a kind of quotes/brackets that is used. – alan2here Dec 12 '16 at 18:03