Witam,
Chciałbym się dowiedzieć co znaczy operator "$" w przykładzie:
Double wynik = p.Dodaj(a, b);
Console.WriteLine($"wynik = {wynik}");
Console.ReadLine();
Bo możesz napisać
string platki = "platki"
string mlekiem = "mlekiem"
string x = "Jem sobie {0} z {1}", platki, mlekiem;
a mozesz dac dolara i wpisac to poza 0 i 1 co jest czytelniejsze (lub nie, zalezy co kto preferuje :)
czyli
string x = $"Jem sobie {platki} z {mlekiem}" gdzie platki/mlekiem to są pola.
froziu napisał(a):
Bo możesz napisać
string platki = "platki"
string mlekiem = "mlekiem"
string x = "Jem sobie {0} z {1}", platki, mlekiem;a mozesz dac dolara i wpisac to poza 0 i 1 co jest czytelniejsze (lub nie, zalezy co kto preferuje :)
czyli
string x = $"Jem sobie {platki} z {mlekiem}" gdzie platki/mlekiem to są pola.
Czyli z operatorem "$" mogę bezpośrednio używać nazw zmiennych?
Dokładnie tak.
Może ten przykład będzie ciekawszy:
string one = "jeden"
string two = "dwa",
string three = "trzy",
string zdanie = $"Liczba {one}, wystepuje przed liczba {two}, a liczba {three} jest suma poprzednich dwoch liczb";
Edit.
Przy okazji jeżeli używasz visual studio to możesz sobie zobaczyć jak kolorowana jest składnia bez dolara a jak z.
Możesz sobie spróbować wypisać stringi z dolarem i bez.
froziu napisał(a):
Dokładnie tak.
Może ten przykład będzie ciekawszy:
string one = "jeden"
string two = "dwa",
string three = "trzy",
string zdanie = $"Liczba {one}, wystepuje przed liczba {two}, a liczba {three} jest suma poprzednich dwoch liczb";
Bardzo dziękuję za odpowiedź. Ten przykład jest jeszcze lepszy.
Jeszcze raz bardzo dziękuję :)
Pozdrawiam
guuren napisał(a):
Witam,
Chciałbym się dowiedzieć co znaczy operator "$" w przykładzie:Double wynik = p.Dodaj(a, b); Console.WriteLine($"wynik = {wynik}"); Console.ReadLine();
To nie jest operator tylko znak specjalny oznaczający interpolowany string.
froziu napisał(a):
Bo możesz napisać
string platki = "platki"
string mlekiem = "mlekiem"
string x = "Jem sobie {0} z {1}", platki, mlekiem;
Raczej nie może. Tzn. może, ale to nie tylko nie zadziała, ale się nawet nie skompiluje.
o to chodzi?
Let’s start with what it is. String interpolation is a more convenient syntax for mixing string literals with expressions. It’s code embedded in a string literal. As such, it needs to be expressed in the scope where the expressions are going to make sense. For instance, the example above is a replacement for:
var url = "http://weblogs.asp.net/" + blog + "/" + slug;
In the same way that you would never write this where “blog” and “slug” are not defined, you can’t write the string interpolation version either: the compiler will just scream at you.
That’s because the literal is parsed at compile-time, which also means that the string interpolation can’t be used on non-literal strings. That alone creates strong constraints on the scope of the feature.
In theory, you can use string interpolation every time you are feeding a literal string into String.Format, or concatenating literal strings and expressions. Such legitimate scenarios includes URL building like above (with sanitized or safe pedigree token values), and similar path building exercises where the format string is not English, but rather a technical kind of string.
Mały Programista napisał(a):
żadnych pomysłów?
Na to o co chodzi z tym wstrzykiwaniem kodu....
Zarejestruj się i dołącz do największej społeczności programistów w Polsce.
Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.