c# method error CS1056

Category: c# general

Question

Vamshi K J on Tue, 17 Dec 2019 03:39:43


Hi,

When i am executing MSBuild I am facing issue "error CS1056: Unexpected character '$' " in the below method.  please provide inputs to use string.Format in the method instead of  $  to solve the issue.

 public static string AdjustHourMinutes(this string sender)
        {

            if (!sender.Contains(":"))
            {
                return sender.Length == 1 ? $"0{sender}:00" : $"{sender}:00";
            }
            else
            {
                var parts = sender.Split(':');
                return parts[1].Length == 2 ? $"{Convert.ToInt32(parts[0]):D2}:{parts[1]}" : 
                    $"{Convert.ToInt32(parts[0]):D2}:0{parts[1]}";
            }
        }

Thank you.


Vamshi Janagama

Replies

Viorel_ on Tue, 17 Dec 2019 06:34:11


An expression like $"0{sender}:00" can be converted to string.Format( "0{0}:00", sender).

 

Try this too:

 

public static string AdjustHourMinutes( this string sender )

{

   string [ ] parts = sender.Split( ':' );

 

   return string.Format( "{0:00}:{1:00}", Convert.ToInt32( parts[0] ), parts.Length > 1 ? Convert.ToInt32( parts[1] ) : 0 );

}

 


Ehsan Sajjad on Tue, 17 Dec 2019 06:47:41


The following post explains the reason and solution for it :

https://stackoverflow.com/questions/42932577/error-cs1056-unexpected-character-running-the-msbuild-on-a-tfs-continuous-i