↑Top

Modern Webpage Tutorial

C# Basics

C# (CSharp) is a powerful coding language used to create web apps, desktop software, video games and much more!

Intro to C# (C-Sharp)

A C# program is structured into classes, methods, statements, expressions, and variables. At its core, every C# application starts with a Main method, which serves as the entry point for the program execution.

The syntax of a C# program is as follows:

using System; ← This is the

class HelloWorld ← This is the class that names the title of our program, beginning the structure.

{

static void Main() ← This is the method that is used to start our program and holds the body for our programs.
{

Console.WriteLine("Hello, World!"); ← This is the class "Console" line of code that prints out to the command line or terminal.
}

} ← This is the Closing Brace for the entire program.

Data Types, Variables, Operations, Type Conversion

In C#, primitive data types represent the most basic types of data that the language supports. These include: Integer, Floating-Point, Character, Boolean Types. Each data type has specific ranges and uses, depending on the size and precision required for storing data.

Variables

Variables in C# are containers that hold data of a specific type. To declare a variable, you specify the type followed by the variable name. Variables must be initialized before they can be used.

int cost; ← this is a declaration.
cost = 200; ← this is an initialization.

Console.WriteLine(cost); ←

You are able to declare and initialize in the same line of code:

int tirePress = 28; ← variable tirePress has been declared and initialized.
Console.WriteLine(tirePress);

Operations

Operators are symbols that perform operations on variables and values. C# supports various types of operators:

  • Arithmetic Operators: +, -, *, /, %
  • Relational Operators: ==, !=, <, >, <=, >=
  • Logical Operators: &&, ||, !
  • Assignment Operators: =, +=, -=, *=, /=, %=
  • Increment and Decrement Operators: ++, --
  • Bitwise Operators: &, |, ^, ~, <<, >>

Type Conversion

Type conversion (also known as casting) in C# is the process of converting a value from one data type to another. Implicit conversion is done automatically by the compiler when there is no risk of data loss. Explicit conversion requires explicit casting and may involve data loss.

int num1 = 10;

double num2 = 5.5;

int result = num1 + (int)num2; // Explicit casting from double to int