C# FULL Beginners Crash Course
JustAWalrus (1181)

C# Full Beginners Crash Course

Hello fellow replitors.

This course is dedicated to me.

Anyway.

Let's get started.

Table of contents

  • Introduction
  • Setup
  • Overview of a basic C# file
  • Hello, World!
  • Variables
  • Basic OOP Overview
  • Input
  • Functions
  • IF Statement
  • WHILE Loop
  • FOR Loop
  • Interesting Features
  • In-Depth OOP
  • Where to go from here
  • Conclusion
  • Bibliography

Introduction

C# is an Object-Oriented, strongly typed, and overall great language developed by Microsoft in 2000.

It is kind of a Java-Wannabe.

In terms of this it wants to be cross-platform but barely.

It works great on windows but not so well on other platforms.

Today we will be focusing on the cross-platform side of C#

It is still a great language though.

Disclaimer!

This course is accelerated so I will not be going in depth in many topics, for instance what an array is. Mainly this tutorial will be HOWs and all WHYs will mainly be about best practices and not metaphors.

This course is aimed at people with a Python, Java, C, or C++ background.

If you know HTML, CSS, JS, TS, Ruby, or anything else you can still learn but it may be slightly harder.

The course will NOT teach you an IDE you can use repl.it or learn an IDE but I am not here to teach you an IDE.

But without further ado let's get a move on.

Setup

For NOW use repl.it but later I'd recommend using a real IDE and or compiler.

Overview of a basic C# file.

using System;

class MainClass {
  public static void Main (string[] args) {
    <code>
  }
}

Let's break down the file.

using System;? Well that is kind of like a #include or import statement. It's how we include new commands in our program.

The MainClass class is well, a class.

If you have no idea what a class is here is a basic overview.

Think about you driving a car the car has attributes, speed, brand, model, ect.

This car also has methods like starting it up, moving it, ect.

So, this is kind of like a class.

The class has methods and attributes,

Kind of like a data type.

The data type int might have a method called makeNegative() and an attribute called value.

Does that make sense?

Anyway public static void Main (string[] args) wow that is a mouthfull.

This is an example of a method!

The public is an identifier saying who can access this variable. See private.

The static means that the value of this method can never change.

The void means that this method returns nothing.

Main is the name. You must have a main class in your program for it to run and compile.

The (string[] args) is hard to explain if you don't come from a Java background.

When the user runs the program, they can specify args: myprogram.exe arg1 arg2.

And it turns them into a string array.

So you can reference them in your function!

Make sense?

Hello, World!

Okay finally, results!

So printing to the console is as easy as this:

Console.WriteLine("Hello, World!");

Notice that most lines in C# end with a semicolon or a bracket.

Also C# is cAsE seNsiTivE so remember that.

The Console is a class in the System library that we imported using using System;.

And WriteLine is a method of Console.

This method takes a string type as an argument. We will go in depth on data types in the next section.

Variables

In this section we will discuss Variables and Data Types in C#

Data Types

  • int , a 32-bit signed integer
  • float , a 32-bit signed floating point value.
  • long , a 64-bit signed integer
  • double , a 64-bit signed number (decimal or not)
  • byte , a 8-bit signed integer
  • bool , a True, False value (1 byte)
  • char , a single unicode character a, b, c, ect. Surrounded by 's
  • string , multiple char types abc, def, ghi, ect. Surrounded by "s

Declaring Variables without giving them an immidiate value.

Consider the code below

int x;

The syntax is <type> <name>;.

Declaring & Assigning a variable.

int x = 1;

The syntax is <type> <name> = <value>;.

Examples of various variable assignments

Some types in C# have special ways to assign them.

Lets review these.

Float

float y = 1.5f;

We put an f at the end because the value defaults to a double type.

Double

double x = 5.5;

Char

char y = 'a';

We put 's around the value to indicate that we are using a char type.

String

string x = "Hello World";

We put "s around the value to indicate that we are using a string type.

That's about it for basic variables.

Basic OOP Overview.

OOP or Object Oriented Programming is the fork of programming revolving around classes and such.

If you do Java or C++ this is probably engraved into your mind anyway.

We discussed Classes a bit in the section Hello, World!.

They have methods and attributes.

Terminology

  • Member - (Methods & Attributes)
  • Instance - A object using that class
  • Method - a function in that class
  • Attribute - a sort of identifier but not really.

We will review how to make classes, methods, ect. in the section In-Depth OOP.

Input

Short section but still important.

So to read a line we can do.

Console.ReadLine();

And this will return a value of type string that is the input.

So we can do.

string x = Console.ReadLine();

To read a single key without the user having to press enter afterword we can do:

char y = Console.ReadKey();

And it returns a char.

Functions

Okay, functions or methods are really easy.

Identifiers

Identifiers are things that can tell the compiler who can access this class/method, if it can ever change, ect.

Let's go over a few.

Access Modifiers

  • public - everyone can access this.
  • private - can only be accessed in the class itself
  • internal - can only be accessed in the current project assembly.
  • protected - can only be accessed within the class or any classes that inherit from the class

Other Modifiers

  • static - only instanciated once and never reinstated.
  • abstract - tells the compiler that the object being changed has a missing or incomplete implementation.
  • virtual - used to change a method, property, indexer, or event declaration and allow for it to be overridden in a derived class.
  • const - used with variables to declare that its value can never change

Syntax

Consider the example below:

private static void ex1 (string n) {
    Console.WriteLine("Hello" + n + "!");
}

The syntax is <modifiers> <return_type> <name> (<args>)

Darn that is a headache, let's break this down.

The modifiers are things like protected or abstract.

The return type is what the function returns (data type).

The return type in the example above is void that means that the function returns nothing.

The name is a way to reference the function.

The args have their own syntax:

<type> <name>

They are function-scope variables.

The code in the function must be in brackets.

IF statement.

Logic, finally.

Okay so if you are not familiar with an if statement, then learn more of your best language then come back here.

But we can show you syntax here:

if (<condition>)
{
    <code that executes if the condition is true>
}

Operators

  • != - Does not equal
  • == - equals
  • >= or => - greater than or equal to
  • <= or =< - less than or equal to.
  • > - greater than
  • < - less than
  • && - and
  • || - or

Else if and Else

Else if has the same syntax as if, it executes if the prior if and or else if statements weren't executed but this condition is true.

Else statements executes if the prior if and else if statements werent true:

Syntax:

Else {
    <code>
}

WHILE Loop

As much as I despise the WHILE Loop and encourage the FOR Loop most people love the WHILE Loop.

So I need to cover it.

The syntax is:

while (<condition>) 
{
    <code block to be executed>
}

And the same operators can be used in the WHILE Loop as well as in the IF statement.

That is pretty much it, haha.

FOR Loop

for (<statement 1>; <statement 2>; <statement 3>) 
{
    <code block to be executed>
}

Statement 1 runs one time before the execution of the code.

Statement 2 defines the condition for executing the code.

Statement 3 runs every time after the code is executed.

Interesting Features

C# .NET comes with many different features.

Let's Look at a few.

Beep.

Console.Beep()

It makes a beeping sound. Doesn't work on repl.it.

Yep.

<value>++;

i++;

OR

<value>++;

It increments the value by 1.

In-Depth OOP

Okay this section is going to be really big. Just kiding I'm going to make it as quick and simple as possible

Classes

I hope at this point you understand what a class is.

Syntax

<modifiers> class <name> {
    <code>
}

The modifiers are the same as in functions section.

Methods.

// These are just functions

Okay.

Attributes

As defined in Basic OOP Overview. these are kind of like indentifiers except not.

They are a whole other fork and I won't really show you how to make them but I want you to know what they are.

But I'll show you an example from the Microsoft Documentation:

[System.Runtime.InteropServices.DllImport("user32.dll")]
extern static void SampleMethod();

A method with the attribute DllImportAttribute is declared.

Okay so yeah they are kinda difficult.

Inheritance

Take this example from the Microsoft Documentation:

using System;

public class A
{
   private int value = 10;

   public class B : A
   {
       public int GetValue()
       {
           return this.value;
       }
   }
}

public class C : A
{
//    public int GetValue()
//    {
//        return this.value;
//    }
}

public class Example
{
    public static void Main(string[] args)
    {
        var b = new A.B();
        Console.WriteLine(b.GetValue());
    }
}
// The example displays the following output:
//       10

Inheritance is the act of one class inheriting from another.

Again a very in-depth field.

Where to go from here.

I'd suggest making a few programs and checking out the Microsoft Documentation.

C# is a very expansive language. And there is so much you can do with it.

There are so many jobs in C#

But if you wanted to learn more from me comment down below that you want to see an Intermediate Course.

Conclusion

Well. Upvote, feedback.

Bibliography

Works Cited

BillWagner. “C# Docs - Get Started, Tutorials, Reference.” Microsoft.Com, 2019, docs.microsoft.com/en-us/dotnet/csharp/.

“C# Tutorial - Tutorialspoint.” Www.Tutorialspoint.Com, www.tutorialspoint.com/csharp/index.htm.

“C# Tutorial (C Sharp).” W3schools.Com, 2019, www.w3schools.com/cs/.

You are viewing a single comment. View All
Highwayman (1459)

Ye write-up lol sry bout that typo XD @johnstev111