Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass interpolated strings as parameter to a method?

I have started to use Interpolated Strings (new feature of C# 6) and it is really useful and gracefully. But according to my needs I have to pass format of string to a method as a parameter. Something like next:

MyMethod(string format)

In the past, I used it in the next way:

MyMethod("AAA{0:00}")

Now I tried this code:

MyMethod($"AAA{i:00}")

But this doesn't work, because i is created inside of the method and is out of scope in this context.

Is it possible to use any trick for passing interpolated strings as a parameter to a method?

like image 574
RredCat Avatar asked Aug 13 '15 11:08

RredCat


3 Answers

You cannot do that, and that would not be a very good idea either - it means you are using local variables from another method.
This would defeat the purpose of this feature - to have compiler verifiable string interpolation and binding to local variables.

C# has several good alternatives. For example, using a Func:

public void MyMethod(Func<int,string> formatNumber)
{
    int i = 3;
    var formatted = formatNumber(i);
}

use as:

MyMethod(number => $"AAA{number:00}");

This is better than what you have today - where you have the format string(s?) and its usage in different places.

If you have more than a single variable this approach can scale, but consider grouping them into a class (or a struct) - the func will look much better, and your code will be more readable. This class can also override .ToString(), which might be useful for you.

Another simple option is to pass just the format: MyMethod("00") and i.ToString(format), but that only works in your presumably simplified example.

like image 143
Kobi Avatar answered Nov 16 '22 12:11

Kobi


There are several ways you can do this.

The first one is to add another method overload that takes a FormattableString (a new typed used by one of the variants of string interpollation) and calls the original one:

public void MyMethod(FormattableString fs)
{
    MyMethod(fs.Format);
}

And you also have access to the arguments if you need to.

If you only need the format, just create an extension method to extract it:

public static string AsFormat(FormattableString fs)
{
    return fs.Format;
}
like image 7
Paulo Morgado Avatar answered Nov 16 '22 13:11

Paulo Morgado


Several years since this was asked however I came across this via Google while searching for an answer that I already had in my head, but couldn't get my mind around the implementation/syntax.

My solution is further to what @Kobi supplied...

public class Person
    {
        public int Id {get; set;}
        public DateTime DoB {get;set;}
        public String Name {get;set;}
    }

public class MyResolver
{
    public Func<Person, string> Resolve {get;set;}
    public string ResolveToString<T>(T r) where T : Person
    {
        return this.Resolve(r);
    }
}

// code to use here...
var employee = new Person();
employee.Id = 1234;
employee.Name = "John";
employee.DoB = new DateTime(1977,1,1);

var resolver = new MyResolver();
resolver.Resolve = x => $"This person is called {x.Name}, DoB is {x.DoB.ToString()} and their Id is {x.Id}";

Console.WriteLine(resolver.ResolveToString(employee));

I haven't tested the above for syntax errors but hopefully you get the idea and you can now just define "how" you want the string to look but leave the blanks to be filled in at runtime depending upon the values in your Person object.

Cheers,

Wayne

like image 1
Wayne Feltham Avatar answered Nov 16 '22 12:11

Wayne Feltham