DEV Community

loading...

New stuff on C# I learned today

carlosch profile image CarlosC ・1 min read

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.

Variable

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.

C#

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

JavaScript

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.

Discussion (1)

pic
Editor guide
Collapse
ashutoshkrris profile image
Ashutosh Krishna

Hi
Loved your writing
I'd like to feature it on my blog site iRead
I don't mind you adding backlinks to your original writings/works.

About iRead :
iRead is an open platform where readers find dynamic thinking, and where expert and undiscovered voices can share their writing on any topic.
Know More : iread.ga
Watch Demo : youtu.be/jLdJnVgpV8U