Unit 1: Introduction to C# PRACTICAL UNDERSTANDING Flashcards
SYNTAX AT THE BEGINNING OF THE PROGRAM
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
using System
means that we can use classes from the System namespace.
A blank line.
C# ignores white space. However, multiple lines makes the code more readable.
namespace
is used to organize your code, and it is a container for classes and other namespaces.
The curly braces {}
marks the beginning and the end of a block of code.
class is a
container for data and methods, which brings functionality to your program.
Every line of code that runs in C# must be inside a class. In our example, we named the class Program.
Another thing that always appear in a C# program, is the Main method.
Any code inside its curly brackets {} will be executed.
You don’t have to understand the keywords before and after Main. You will get to know them bit by bit while reading this tutorial.
If you omit the using System line
you would have to write System.Console.WriteLine() to print/output text.
Console is a class of the
System namespace,
Every C# statement ends with a
semicolon ;
“MyClass” and “myclass” has different meaning. BECAUSE
C# is case-sensitive:
Single-line comments start with
two forward slashes//
Multi-line comments start with
/*
Multi-line ends with
*/
To output values or print text in C#, you can use the
WriteLine() method
This will output Hello World….?
Console.WriteLine(“Hello World!”);
Console.WriteLine(9 + 3);
This will output 12
There is also a method, which is similar to WriteLine().
Write()
The only difference is that it does not insert a new line at the end of the output
Write()
IT PRINTS ON THE SAME LINE
stores integers (whole numbers), without decimals, such as 123 or -123
int
stores floating point numbers, with decimals, such as 19.99 or -19.99
double -
stores single characters, such as ‘a’ or ‘B’. Char values are surrounded by single quotes
char
- stores text, such as “Hello World”. String values are surrounded by double quotes
string
- stores values with two states: true or false
bool
To create a variable, you must specify the type and assign it a value:
WHAT IS THE SYNTAX
type variableName = value;
type variableName = value;
Where type is a C# type (such as int or string), and variableName is the name of the variable (such as x or name). The equal sign is used to assign values to the variable.
Create a variable called name of type string and assign it the value “John”:
string name = “John”;
Console.WriteLine(name);
Create a variable called myNum of type int and assign it the value 15:
int myNum = 15;
Console.WriteLine(myNum);
You can also declare a variable without assigning the value, and assign the value later:
int myNum;
myNum = 15;
Console.WriteLine(myNum);
int myNum = 15;
myNum = 20; // myNum is now 20
Console.WriteLine(myNum);
if you assign a new value to an existing variable, it will overwrite the previous value:
A demonstration of how to declare variables of other types:
int myNum = 5;
double myDoubleNum = 5.99D;
char myLetter = ‘D’;
bool myBool = true;
string myText = “Hello”;
If you don’t want others (or yourself) to overwrite existing values…
you can add the const keyword in front of the variable type.
If you don’t want others (or yourself) to overwrite existing values…
const int myNum = 15;
myNum = 20; // error
string name = “John”;
Console.WriteLine(“Hello “ + name);
To combine both text and a variable, use the + character:
string firstName = “John “;
string lastName = “Doe”;
string fullName = firstName + lastName;
Console.WriteLine(fullName);
You can also use the + character to add a variable to another variable:
int x = 5;
int y = 6;
Console.WriteLine(x + y); // Print the value of x + y
For numeric values, the + character works as a mathematical operator (notice that we use int (integer) variables here):