Using C # 6 string interpolation

7

With C # 6 and using interpolation it was better to concatenate string with data.

As in the example.

Instead of:

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

It looks much better

$"{d} anos";

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

I have an immovable object with its properties.

When performing direct interpolation as:

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

It works perfectly.

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

And through a foreach I need it to work, does anyone have any idea how to solve it?

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: link

    
asked by anonymous 11.05.2016 / 23:20

2 answers

3

follows 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; } 
}

link

    
13.05.2016 / 02:11
2

You want to get the value of the object normally, but the name of the variable should be the same as 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 retrieves the value of the object outside the list, just have the same name as the property you are passing in the text.

See working in .NetFiddle.

The way you are concatenating the text that does not work: var meuTexto = $""+ t; . It tries to apply interpolation before the + sign, that is, before the text {imovel.Nome} .

    
11.05.2016 / 23:40