Introduction to the syntax and debugging
//
for inline and /* */
for multiline.ctrl
+ K
C
, to uncomment ctrl
+ K
U
Ctrl
+ /
if () {} else {}
is the same;
are not optionalusing
statement in C# is the JavaScript import
equivalent{}
on their own lines is a C# style thingvoid
means we are not returning any valuescw
is the snippet shortcut for Console.WriteLine
ctrl
+ .
is the shortcut to add using statements1if (args.Length > 0)
2{
3 Console.WriteLine($"Hello, {args[0]}!");
4}
5else
6{
7 Console.WriteLine("Hello stranger!");
8}
In VS Code
F5
will start debuggingCtrl
+ F5
will start without debuggingShift
+ F5
will stop without debuggingInstead of console.log()
, you get Console.WriteLine()
. This Console
class is much better though, because you can also take user input, i.e. Console.ReadLine()
.
It’s better if you use the debugger though, that can show you whatever values you’re getting in any point in time via intellisense
using
is like the ES6 import
statement. It just means you’re referencing stuff from other classes and namespaces.
using System;
let’s you do Console.WriteLine("Hello stranger!");
since Console is actually System.Console
1// Javascript
2import 'module-name'
or like Python
1#!/usr/bin/python
2
3import sys
4print sys.version
Concatenation is simple, you use the +
sign
For interpolation, you do $" {variable} "
. The $
sign is only used once, outside the entire string
1// concatenation
2"Hello " + args[0] + "!"
3
4// interpolation
5$"Hello, {args[0]}!"
String interpolation is what is called Template literals or template strings in JavaScript
1$"Hello, {args[0]}!"
is the JS equivalent of
1;`Hello, ${args[0]}!`
1void // returns nothing
2string[] // string array
3double[] grades // array called 'grades' containing 'double' value type
4List<double> // a List of type double
5float // floating point values
6double // double precision floating point values, like financial software precise, takes up more storage space than float
1int x = 34; // explicit type, explicitly tell what type it is
2var y = 34; // implicit type, let compiler figure out type
Implicit typing with var
is nice because it can automatically determine the type and looks neat. Some people prefer it, some don’t.
var
is still strongly types though, because var
only works when you are initialize the value.
1var z = 87.2; // var figures out type
2
3z = "string"; // illegal!
1var z; // implicit typing won't work
2z = 87.2;
1double[] numbers; // declare array
2double[] grades = new double[3] // initialize array and specify the no. of elements it'll contain
1double[] grades = new double[3]; // explicit typing
2var numbers = new double[3]; // implicit typing
1// array initialization syntax
2var number = new double[] {13.2, 29.1, 22.1, 33.1};
1var number = new[] {13.2, 29.1, 22.1, 33.1}; // compiler will figure out array size and array type
When also initializing the array with values, you don’t need to provide the size of the array, the compiler will figure out how many by the amount of initial values you provided
1foreach (double grade in grades)
2{
3 // code goes here
4}
Arrays don’t dynamically grow. That means they are impossible to use when we don’t know the quantity of values the array is going to hold.
Unlike arrays, you can keep adding values into a list with the .Add()
method
1List<T> // T = type of elements in the list
1List<double> grades = new List<double>(); // explicit typing
1var numbers = new List<double>(); // implicit typing
1List<double> grades = new List<double>() { 13.2, 29.1, 33.1 };
Notice the ()
at the end.
1{
2 List<double> grades = new List<double>() { 13.2, 29.1, 33.1 };
3
4 grades.Add(52.1);
5
6 var result = 0.0;
7
8 foreach (double grade in grades)
9 {
10 result += grade;
11 }
12
13 var averageGrade = result / grades.Count;
14
15 Console.WriteLine($"The average grade is: {averageGrade:N1}"); // The average grade is 31.8
16}
:N1
is a format specifier, it means show 1 number after the double, i.e. 31.8
instead of 31.875