In this C# Crash Course, we'll go over the basics of C# so that you'll be ready to build out exciting web apps! We'll start by going through the key attributes of C#, syntax basics, and introduce you to OOP. In each section, we'll link you to some quick in-browser C# challenges so you can apply these concepts.
If you're completely new to C# and want a more comprehensive learning path, check out our C# Curriculum. The projects included in that curriculum are listed below. You can open this repository as a Codespace to complete those projects.
- Language attributes
- Syntax basics
- Object Oriented Programming
C# is a strongly typed, compiled, object oriented language. Let's break this down.
In a strongly typed language, every variable has a defined type. Some of these types include:
- String, "Hello world!"
- Char, 'a'
- int, 3
- decimal, 1.5
- bool, True
A compiler converts the code you write into a format that your computer can understand. After you write C# and build it, the C# compiler (called Roslyn) will analyze your code to check for any errors.
Here's a piece of code that will print "Hello world!" to the console.
using System;
Console.WriteLine("Hello world!");
With C#, you use keywords like using
and Console
.
Keywords are predefined, reserved identifiers that have special meanings to the compiler.
The . (DOT)
in Console.WriteLine
allows us to access methods and properties. In this example, Console
is a type that represents the console window. WriteLine
is a method of the Console type that prints a line of text to that text console.
In this example, we use parentheses pass a string as a parameter to Console.WriteLine
.
Time for your first challenge!
# | Challenge | Solution | Duration | What you will learn | More information |
---|---|---|---|---|---|
1 | Hello World Challenge | N/A | 3 min | case sensitive, strings, comments | Intro to C# Tutorial, C# documentation |
In C#, variables allow you to temporarily store a value in memory. In C#, you must declare a variable before using it.
var cSharp = "really cool";
In this example, we created a string called cSharp
. You can use the var keyword to declare local variables without explicitly giving them a type.
Variable names can contain alphanumeric characters and underscores, but no special characters. They also cannot be keywords.
# | Challenge | Solution | Duration | What you will learn | More information |
---|---|---|---|---|---|
2 | Variables Challenge | Solution | 5 min | variables, data types, strings, ints, decimals | String formatting tutorial, C# documentation |
Every statement is ended by a semicolon
Console.WriteLine("there is a ';' at the end of this statement");
You can make comments by using 2 slashes
// this is a comment is C#
C# is case sensitive! For example, a variable "cat" is completely different from a variable "CAT".
var cat = "meow";
var CAT = "rawr";
These are probably familiar to you!
symbol | what it does |
---|---|
+ | addition |
- | subtraction |
* | multiplication |
/ | division |
% | remainder |
++ | increment |
-- | decrement |
# | Challenge | Solution | Duration | What you will learn | More information |
---|---|---|---|---|---|
3 | Operating on numbers challenge | Solution | 2 min | ints, decimals | Number operations tutorial, C# documentation |
In C#, you can build applications that employ decision-making logic so that your application performs different instructions based on a set of conditions. One way we do this is with an if
statement. if
statements are made up of three parts:
- The if keyword
- A Boolean expression between parenthesis ()
- A code block defined by curly braces { }
If there are multiple condition, you can utilize the else if
and else
statements. Basically, if the if
statement fails, these other statements allow you to test against other conditions.
You can imagine this in the context of a rock⛰️-paper📃-scissors✂️ game. Imagine you chose rock⛰️.
if
your opponent chooses scissors✂️, then you will winelse if
your opponent also chooses rock⛰️, then you will tieelse
your opponent chooses paper📃, then you will lose
Each possible decision your opponent could make leads to a different outcome.
Booleans are expressions that return either true
or false
. They are often used to compare two or more things.
symbol | what it does |
---|---|
< | less than |
> | greater than |
<= | less than or equal |
>= | greater than or equal |
== | equal |
!= | not equal |
# | Challenge | Solution | Duration | What you will learn | More information |
---|---|---|---|---|---|
4 | Decision logic challenge | Solution | 2 min | if, else if, else, booleans | Boolean expressions tutorial, C# documentation |
C# is an object-oriented language. Objects are defined by Classes. In other words, an Object is an instance of a class. One way to think about this is that a class is like the blue prints for a house. The actual house that is built is an objects because it is an instance of this blue print.
Objects inherently have attributes. In C# we call these properties. The attributes of a house may be the number of doors, what color the house is painted, etc.
We can also define methods which describe what an object can do. For example, you can sell your house.
To summarize these concepts using our house example, A Class is like a blueprint 📜 An Object is an instance of this blueprint, or a house 🏠 The Properties of a house could be the number of doors it has or the color it's painted 🚪 🎨 A Method of our class is that we can sell our house 💸
Let's look at an example House class:
// The namespace declaration provides a way to logically organize your classes
namespace Classes;
public class House
{
// House properties
public string Address { get; }
public int Size { get;}
// House methods
public void SellHouse(decimal amount, DateTime date)
{
}
}
We can define a constructor to allow us to create new House objects.
public House(string address, int squareFeet)
{
this.Address = address;
this.Size = squareFeet;
}
When we create an object with new
this constructor will be called.
using Classes;
// Let's create a 1500 square foot house on Candy Cane Lane
var house = new House("123 Candy Cane Lane", 1500);
C# also has built in classes and functionality within the .NET Class Library. The .NET Class Library is a collection of thousands of classes containing tens of thousands of methods. These methods are created by Microsoft and are available for use in your applications. For example, when we called Console.WriteLine
earlier, we were calling a method from the System.Console
class. For a more in-depth overview, you can read up on the .NET Class Library in the .NET documentation.
# | Challenge | Solution | Duration | What you will learn | More information |
---|---|---|---|---|---|
5 | .NET Class Library challenge | Solution | 2 min | ints, decimals | .NET documentation, C# documentation |
Want more practice with C#? The .NET team has you covered. Here's a few learning resources:
- C# Video Series on Microsoft Docs or YouTube
- Self Guided Tutorials on Microsoft Learn
- Learn to Code Page
Connect with us! Check out the .NET Community Page to find links to our blogs, YouTube, Twitter, and more.
Please take this quick, 10 question survey to give us your thoughts on this lesson & challenge!