SoFunction
Updated on 2025-03-06

Detailed explanation of string splicing in C# @ $

one:@

@ means to use characters marked by @, where all symbols are string symbols, and there are no special characters, such as '', etc., all default to strings.

    static void Main(string[] args)
    {
    //This makes it very convenient to write the read file path, and there is no need for escape characters     StreamReader sr = new StreamReader(@"C:\Users\WANGWI8\Desktop\");
     string line;
     while( (line = ()) != null )
     {
       (line);
     }
     }

Two: $

$ means that in the string marked with it, the value can be taken by brackets:

static void Main(string[] args)
    {
      var name = ();
      var date = ;
      var result = $"hello {name} , you are my father , i am your son , writed at {date}";
      (result);
      var item2 =
      (
        name: "william",
        sex: 'f'
      );
      ($"this is item :  : {} , this is sex : {}");
      var inventory = new Dictionary<string, int>()
      {
        ["hammer, ball pein"] = 18,
        ["hammer, cross pein"] = 5,
        ["screwdriver, Phillips #2"] = 14
      };
      ($"inventory on {:d}");
      (" ");
      //The negative sign means left alignment, occupying a total of 25 characters of space      ($"|{"item",-25}|{"quantity",10}|");
      foreach (var item in inventory)
        ($"|{,-25}|{,10}|");
    }

Summarize

The above is the string splicing @ $ introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!