Using string interpolation C# 6

Asked

Viewed 3,695 times

7

With C# 6 and interpolation usage is better than concatenating string with data.

As in the example.

Instead of:

var d = "19";
string.Format("{0} anos", d);

looks much better

$"{d} anos";

But using the feature, I created a column in my database which will be fed by the user through object.

I have an immovable object with its properties.

When performing direct interpolation as:

$"{imovel.Nome} aqui é meu imóvel";

Works perfectly.

But now I have this information "{imovel.Nome} aqui é meu imóvel" in a column.

And through a foreach I need it to work, someone has some idea of how to solve?

Ex:

var imovel = new Imovel().GetImovel(); // aqui retorna meu objeto imovel
foreach(var t in Textos){
 var meuTexto = $""+ t; // aqui tem isso "{imovel.Nome} aqui é meu imóvel"
 var TextoInterpolado = meuTexto; // aqui deveria formatar Casa1 aqui é meu imóvel, mas ele mostra apenas assim "{imovel.Nome} aqui é meu imóvel"
}

I do not know if it was very clear, but I want something like this example: https://dotnetfiddle.net/Zk3nbL

  • The answer is simple: You can’t do it that way. But it can be done this way

2 answers

3

follow the expected return, a friend replied by email.

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var listaImoveis = new List<Func<Imovel,string>>{
            imovel => $"{imovel.Nome} aqui é meu imóvel",
            imovel => $"{imovel.Numero} aqui é meu imóvel",
            imovel => $"{imovel.Complemento} aqui é meu imóvel"
        };      

        var imovelVai = new Imovel { Nome = "Casa X", Numero = "10", Complemento = "Casa X"};       
        foreach(var t in listaImoveis)
            Console.WriteLine(t(imovelVai));
    }
}

public class Imovel
{   
    public string Nome{ get; set; }
    public string Numero{ get; set; }
    public string Complemento{ get; set; } 
}

https://dotnetfiddle.net/b15fYK

  • Just like the one I posted 23 hours ago as a comment on your question. Looks like he came to the same solution as me, since my fiddle has no views.

  • That’s right @Bruno Costa, I didn’t see your comment, why didn’t you post as a direct response? But that’s right, you killed the riddle!

2

You want to get the object value normally, but the variable name must be equal to the name you have in your text, for example:

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var listaImoveis = new List<Imovel>{
            new Imovel{Nome = "Casa1"},
            new Imovel{Nome = "Casa2"},
            new Imovel{Nome = "Casa3"}
        };
        //var imovel = new Imovel{Nome = "Casa4"};
        //  foreach(var t in listaImoveis){
        //  var texto = $"{imovel.Nome} aqui é meu imóvel";
        //  Console.WriteLine(texto);
        //}

        foreach(var imovel in listaImoveis){
            var texto = $"{imovel.Nome} aqui é meu imóvel";
            Console.WriteLine(texto);
        }
    }
}

public class Imovel
{
    public string Nome{get;set;}
}

In this example, you are replacing the value of the list, but if you uncomment the code, you will see that it also recovers the value of the object outside the list, just have the same name as the property you are passing in the text.

See working on . Netfiddle.

The way you’re concatenating text that doesn’t work: var meuTexto = $""+ t;. He tries to apply the Interpolation before the sign of +, that is, before the text {imovel.Nome}.

  • I know how interpolation works. But understand my situation. in your foeach, you scroll through the buildings and print on the screen. In mine, I go through a list of strings, which following your example would be: "{immovable.Name} here is my property", "{immovable.Number} here is my property number". Got it?

  • As in this example: https://dotnetfiddle.net/Zk3nbL

  • 1

    @Ivanteles Then edit your question and explain it. The way it is, it is not to understand that you want this.

  • Randrade added the example of my need, can help me with some idea?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.