0

I'm using String interpolation in the code behind, and now I need to take part of it to a class.

when I do it, I get error "CS1056: Unexpected character '$'"

even a very simple code gives the error right on running (not on build):

string MailSubject = $"this is your score: {userScore}";

this part of code is part of the FaceClass.CS file

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;

namespace ns.App_Code
{
    public class FakeClass
    {
        public static void Check_Next_In_Line(int score)
        {
            int temp = Fake2Class.GetData();

            if (temp == 0)
            {
                string MailSubject = "";
                string MailBody = "";

                MailBody = $"Your score: {score}";

                /*
                mail send function
                */
            }
        }
    }
}

I'm using .NET Framework 4.8 String Interpolation works for me in a aspx code behind but not in a method within a class. If I want to refactor a part of code becuase it is needed more than once - it won't work

Y.G.J
  • 1,098
  • 5
  • 19
  • 44
  • 2
    I think you'll need to provide more context before anyone can help with this. But I assume that your code is being compiled with an old version of C# / .net framework. – Jeremy Lakeman Aug 02 '22 at 04:54
  • 2
    Which .Net Framework/C# version are you using? String interpolation feature is introduced in C# 6.0 (.Net Framework 4.6). – Bhavikms Aug 02 '22 at 04:56
  • Maybe https://stackoverflow.com/questions/42932577/error-cs1056-unexpected-character-running-the-msbuild-on-a-tfs-continuous-i ? – Jeremy Lakeman Aug 02 '22 at 04:57
  • which version of .Net framework/C# you are using? Normally it should work – Amal Ps Aug 02 '22 at 05:13
  • @JeremyLakeman I have tried the answers in the post you linked in the past with no luck, again, my problem is not with string interpolation in my code behind - only in CS file for class methods – Y.G.J Aug 03 '22 at 05:15
  • Sounds like you set your project's language version to C#5. Just right click on the project, go to properties and find the language version drop down, then jack it all the way up... – Aron Aug 03 '22 at 05:34

1 Answers1

0

Hi an alternative solution to what you are looking for may be would be to use string format. Something like below

int userscore;
string MailSubject = string.Format("this is your score: {0}", userscore);
Maddy
  • 774
  • 5
  • 14