Intro to C#

Redmond Chan
3 min readDec 14, 2019

--

I’m currently learning the basics of C# because I may have the opportunity to do some professional work with C#.

For starters, the classic “Hello World” message using Console.WriteLine method.

The argument it takes is a string that you want displayed in the console. A string is a data type, represented by double quotations. The string here is “Hello World” and that will be printed out on to the console. Fairly simple, if you’re familiar with JavaScript, I would say this is the equivalent to console.log.

Now we have variables. On line 8, we first declare the data type followed by the variable name. C# has 6 data types: int, float, double, char, bool, and string. On line 9, the var keyword is used to let the complier determine the data type instead of declaring it yourself.

On line 10, we have composite formatting. This is my first time seeing this method. The numbers within the curly brackets will represent which variable it’ll take after the first comma of the string. The list after the first comma will be separated with commas and the first item on the list is represented by 0. It increments by 1 for the next item on the list. Right now line 10 will display: “I have 100 dogs”. If you switched the numbers around, “I have {1} {0}”, this will display: “I have dogs 100".

On line 11, we have string interpolation. This is the method I’m aware of for using variables within a string. First, you need to add the dollar sign, “$”, in front of the string. Then instead of the numbers in the curly bracket, you’ll put in the variable name. Line 11 would display: I have 100 dogs.

Line 13, will prompt the user to enter an input into the console and then it’ll be set to the variable, yourName. Let’s say I enter in “Bob”, the console will display: Your name is Bob.

This is how you declare a constant. You use the const keyword followed by the data type and then followed by the variable name. Constants can’t be changed so reassigning num to 9 would cause an error. Constants must be initialized with a value.

Color was initialized without a value so this would cause an error.

I won’t go through all arithmetic operators, you should know if if you’ve attended any math classes. Line 26 will add the numbers 1 + 2 to give you c. This would display: 1, 2 , 3. You can replace the addition sign with any other operator. Here’s a more in depth documentation on the operators:

That’s it for now because I don’t want to overwhelm anyone with information. This is all of the basic concepts I can think of, if not all then most. Hope this has helped you understand the basics of C#. I’ll see you on my next post!

--

--