Fall 2020
August 17, 2020
Basic building blocks that are used to construct any program (= software = app = application).
Some of the specificity of C# is that:
There are multiple paradigms (cf. https://en.wikipedia.org/wiki/Programming_paradigm), i.e. “families”, of languages. Object-oriented language is one of them, and Java, C#, Python, … are in that same family. The key concepts (that we will study at length) are:
// Hello!
using System;
class Welcome
{
static void Main()
{
Console.WriteLine("Welcome to PCP!");
}
}
/*
This is a multi-line
comment
*/Only one line actually does something, (Console.WriteLine("Welcome to PCP!");), the rest is “overhead”: for now, let’s take that to be a magical incantation, we’ll expound on that soon.
“Line by line” reading of the code:
Console.WriteLine("Welcome to PCP!"); and using System;. They both end with ;.//…) and delimited, or multi-line, comments (/* … */),Using directive. Namespace (= API, Library, i.e., set of classes)Welcome).Main).General rules for the syntax of C#:
;.Oddly enough, C# does not care (almost at all) about spaces, but the programmer does: this is the difference between rules (use exact case, for instance) and conventions (like indentation, class name starting with an upper-case letter, …). Rules are required by the compilers, conventions are recommended by the programmers (and your instructors!).
There are also rules when we will be writing classes:
Main method is the entry point of a C# application.Some conventions include:
.cs file has the same name as the class inside it.Those are actually rules in Java!
using, class, static, void , namespace, … (Main is not a keyword).Welcome). Class name, variable name, method name, …System, Console, WriteLine): if I want to use what they programmed for us, I have to use the name they picked. They also picked identifiers and gathered them in a namespace (=API, collection of classes we can use).“Rules”: for the compiler.
An identifier…
“Conventions”: for the programmers, common to many programming languages (some of Java’s rules are C# conventions).
ClASs is probably not a good choice).Starting here, your instructor will draw at the board “Output window”, and will stop writing all the code, to focus on the Main method or notion at stake.
In our body, we can have multiple statements:
In addition to Console.WriteLine, we can also use Console.Write: the difference is that the former adds a new line after displaying the message, while the latter only displays the message, without adding a new line.
We can also use escape sequences for special characters:
\n - start a new line (newline character)\a - play a sound (alert)\t - horizontal tabAll of these use the escape character (\).