reuse interpolated strings [duplicate] - c#

This question already has answers here:
String Interpolation with format variable
(7 answers)
Closed 7 years ago.
Has anyone figured out how to reuse an interpolated string?
I.e. can anyone figure out how to do away with the string.Format in the following block of code?
foreach(var s in new[]{ "Primary_{0}_Home", "Secondary_{0}_Work" }){
sql = $"SELECT {string.format(s, "Street")}, {string.format(s, "City")} ..."
}

You can't do that.
Interpolated strings are set on compile time. You can't use string interpolation to load a string to format something not directly in the scope.

Related

C# Add conditional directly inside string [duplicate]

This question already has answers here:
C# interpolated string with conditional-operator [duplicate]
(2 answers)
Closed 4 months ago.
I have a boolean isEuropean and based on it's value I want to write
Console.WriteLine("This individual is/is not European");
Is it possible in C# to add a conditional directly inside a string with no additional variables created?
bool isEuropean = true;
Console.WriteLine($"This individual {(isEuropean ? "is" : "is not")} European");
Yes, you can do string interpolation with a ternary, make sure you contain the ternary in parentheses.

Is possible to add warning symbol in C#? [duplicate]

This question already has answers here:
How to write Unicode characters to the console?
(5 answers)
Closed 4 years ago.
i want to ask if is possible to add in a string the "⚠" character and make it executable in console with Console.WriteLine().
you can use this code;
System.Console.OutputEncoding = System.Text.Encoding.Unicode;
Console.WriteLine("⚠");

How to check a string for a question mark? [duplicate]

This question already has answers here:
How can I check if a string contains a character in C#?
(8 answers)
Closed 5 years ago.
I want to check if an inputted string contains a question mark.
Probably quite simple but I'm new to coding.
Use String.Contains() :
string myString = "Hello world?";
bool containsQuestionMark = myString.Contains("?"); // true
For future references, use MSDN, it's filled with good documentation.
Alternatively (to Rick's answer), if you are checking just for char occurance in a string you can use IndexOf(char):
bool containsQuestionMark = myString.IndexOf('?') != -1;
There are also some minor (negligible) performance differences between two approaches, depending on the framework version being used.

Decimal.Parse() vs Convert.ToDecimal() [duplicate]

This question already has answers here:
What's the main difference between int.Parse() and Convert.ToInt32
(13 answers)
Closed 9 years ago.
I would like to know if there is any difference between the captioned two methods?
string str = "14.75";
Console.WriteLine(Decimal.Parse(str));
Console.WriteLine(Convert.ToDecimal(str));
Can anyone please let me know? Thanks.
Decimal.Parse only supports parsing string values, whereas Convert takes other types as well, like, object, int, byte, DateTime etc.

Difference between bool/Boolean, string/String etc [duplicate]

This question already exists:
Closed 12 years ago.
Possible Duplicate:
String vs string in C#
What is the big difference between these datatypes? and were should I use it?
Short answer, there is no difference. They are just alias of each other, see
http://msdn.microsoft.com/en-us/library/ya5y69ds(VS.80).aspx for a complete list.
Have a look at: String vs string in C#

Categories

Resources