DEV Community


Posted on

New stuff on C# I learned today

Data types

Presentation vs Calculation and Evaluation. I love that C# is a typed language. It's great to have to declare if our variable is a string or an int or even a decimal. It's best to use the string or char data type when handling numbers that aren't used in mathematical calculations. Phone numbers, postal codes, etc. Same goes for bool, which should be used only if working with evaluation of true or false.


Variables are temporary storage containers for data.
Assigning a variable is best at the moment of declaration.
string name = "value".
The var keyword is used differently than in other languages. Using var will implicitly give the variable a type at the moment of declaration, which has to be done simultaneously with initialization.

Verbatim string literals and string interpolation

In the C# world by adding @ at the beginning of the string.
Console.WriteLine(@"Say "hello". Don't escape the /")
String interpolation, similarly to JS, uses the $ symbol. HOWEVER, it's placed differently. It also uses double quotes instead of backticks.


Console.WriteLine($"{greeting} {firstName}!");
Enter fullscreen mode Exit fullscreen mode


console.log(`${greeting} ${firstName}!`)
Enter fullscreen mode Exit fullscreen mode

Combining verbatim and string interpolation can be very useful if you need to escape some characters in your string. In this case, $@ should be pre-fixed to the string.

Top comments (1)

Sloan, the sloth mascot
Comment deleted