Events
29 Aug 2021
Events The Crash Course Introduction One of the coolest features of C# is a type of member called an event . An event is a way for one object to tell other objects that some important thing has happened. Objects that wish to be informed can subscribe to the event to get notified. Declaring an Event An event is just another type of member, like a field, a method, a constructor, or a property.
Extension Methods
29 Aug 2021
Extension Methods Crash Course You can make extension methods by using the this keyword on the first parameter in a static method in a static class: public float Squared(this float value) { ... }. You can call an extension method the normal way (FloatExtensions.Squared(number)) but the real value comes from being able to call it this way: number.Squared(). Introduction Sometimes, you wish a class had a method it doesn’t have.
Threads
29 Aug 2021
Threads The Crash Course Threads let you run multiple things at once within your program. Create a thread with new Thread(MethodToRun). Start a thread with thread.Start(); or thread.Start(parameter);. Wait for another thread to complete with thread.Join();. Use lock (someObject) { ... } to ensure only one thread enters a section of code at a time. Introduction Modern computers have multiple cores, which allow the computer to run many statements simultaneously.
Wrap Up
29 Aug 2021
Wrap Up We’ve reached the end of the C# tutorials, but this only marks the beginning of the start. From here, the next step is to continue to another tutorial set. Perhaps the XNA tutorials or the MonoGame tutorials are your next step, if you’re ready to start making games. Of course, even our C# journey isn’t over yet. This crash course is only enough to get us started. You’ll continue to learn more about C# as you go.
Delegates
28 Aug 2021
Delegates Crash Course A delegate defines a type that can store a method: delegate bool ShipEvaluator(Ship ship);. You can use delegate types like any variable type, but you can assign them values that are the names of methods: ShipEvaluator e = IsDead;. To invoke the method currently in a delegate-typed variable, do this: delegateVariable(argument1, argument2) or delegateVariable.Invoke(argument1, argument2);. The Action and Func delegate types are generic, and you can usually use one of those instead of defining your own.
Exceptions
28 Aug 2021
Exceptions Crash Course Exceptions are objects that represent a problem that the code can’t immediately resolve. When an exception is detected and thrown, normal flow of execution is suspended and a search begins up the call stack for a handler that can address it. If no handler is found, your program will crash. Catch an exception with code like this: try { DoSomethingPotentiallyDangerous(); } catch (Exception e) { FixTheProblem(); }.
Operator Overloading, Indexers, and User-Defined Conversions
28 Aug 2021
Operator Overloading, Indexers, and User-Defined Conversions Crash Course You can overload many of the operators in C# for types you make: public static Point operator +(Point a, Point b) { ... }. You can also define how the indexing operator works for your type: public float this[int index] { get { ... } set { ... }}. You can define both implicit (happens automatically when needed) and explicit (requires a cast) conversions for types you define: public static explicit operator Point3(Point input) { .
Files
27 Aug 2021
Files Crash Course The easiest way to write to a file is File.WriteAllText("file.txt", "This content goes into the file.");. The easiest way to read from a file is string contents = File.ReadAllText("file.txt");. You can also use File.WriteAllLines and File.ReadAllLines which use a string[] instead of a single string. File.Exists checks if a file exists and File.Delete deletes a file. You can also use streams to read and write files a little at a time.
Generics
27 Aug 2021
Generics Crash Course The Motivation for Generics Arrays are great for storing collections of items, but have a rather serious limitation: you can’t change the size of an array after it has been created. That makes it hard to add and remove items from an array. If you want to grow an array, you must make a new array that is one item bigger, and copy everything over.
Interfaces
27 Aug 2021
Interfaces Crash Course An interface defines how the rest of the software can interact with an object without providing any specifics or constraints around how it must work. Defining an interface is similar to defining a class: interface IPlayer { /* members go here */ }. Members of an interface are automatically public and abstract, so you don’t need to mark them as such: void MakeMove();. Implementing an interface is done like a base class (class HumanPlayer : IPlayer { .
Structs
27 Aug 2021
Structs Crash Course A struct is defined much like a class, but defines a custom value type instead of a custom reference type: struct Point { float X { get; set; } float Y { get; set; } }. Use structs for small data-focused types. Otherwise, use classes. Introduction A struct is a concept similar to a class. The difference is that a class defines a new reference type, while a struct defines a new value type.
Polymorphism
26 Aug 2021
Polymorphism, Virtual Methods, and Abstract Classes Crash Course Polymorphism allows a base class to define a method, optionally provide a default implementation for that method, and allow derived classes to override it. To make a method that can be overridden in a derived class, you put the virtual keyword on it: public virtual void DoSomething() { ... }. To override a method in a derived class, use the override keyword: public override void DoSomething() { .
Inheritance
25 Aug 2021
Inheritance Crash Course Inheritance lets you make one class build upon another, “inheriting” all of its members. A class can name another class to inherit from: class Derived : Base { ... }. A derived class can be used anywhere the base class is required: Base thing = new Derived();. The only thing not inherited is constructors, which often require careful management to make sure the derived class points out which constructor to call in the base class.
Properties
25 Aug 2021
Properties The Crash Course A property lets you define getter and setter methods while still using the simpler field-like access. Defining a property looks like this: public int SomeNumber { get => _someNumber; set { if (value < 0) _someNumber = 0; else _someNumber = value; } } A property can then be used like this: someObject.SomeNumber = 3; or Console.WriteLine(someObject.SomeNumber); An auto-implemented property (auto property) can simplify creating a property when you don’t have any specific rules you want to enforce: public int SomeNumber { get; set; }.
Classes
24 Aug 2021
Classes Crash Course You can make large programs by breaking the whole program into small objects, each of which are responsible for a small slice of the system, and which all work together. In C#, each object belongs to a class, and a class defines how objects of that type behave, and what data they need to track to do their job. You define a new class with class ClassName { }.
Enumerations
23 Aug 2021
Enumerations Crash Course Enumerations allow you to define a new type where each possible choice is known and can be named: enum PlayerColor { Red, Blue, Green, Yellow }. Once an enumeration type is defined in your program, you can declare variables of that type: PlayerColor color;. To use the values of an enumeration, do the following: color = PlayerColor.Green;. Introduction In C#, types matter. Every variable, every value, and every expression has a type, and things must all align.
Methods
23 Aug 2021
Methods The Crash Course Methods are a tool that let us define and name a block of reusable code: static void DoSomething() { /* statements here */ }. You an pass data to a method by defining a parameter (static void DoSomething(int someParameter) { /* ... */ }) and then passing the argument to use for that parameter in parentheses (DoSomething(4)). You can have multiple parameters by separating them with commas: static void DoSomething(int a, int b) and DoSomething(4, 14).
Arrays
22 Aug 2021
Arrays The Crash course An array lets you create a place to store many values of the same type all at once. Create an array variable like this: int[] numbers; Create an array value like this: new int[5];, where the number in square brackets is the number of items in the array. Write data to a spot in an array like this: numbers[0] = 15;. The index is in square brackets, and starts at 0.
Decision Making
22 Aug 2021
Decision Making The Crash Course An if statement allows a statement to only run when the conditions are right: if (condition) Console.WriteLine("It's happening!"); A block statement lets you bundle many statements into one: { statement1; statement2; } Use block statements when your if needs to guard many statements. To run an alternative set of statements when the if statement’s condition is false, use an else: if (condition) Console.WriteLine("It's happening!"); else Console.
Loops
22 Aug 2021
Loops The Crash Course Loops let you repeat a section of code while some condition is still true. A while loop specifies the condition and checks it first: while (condition) { /* ... */ } A do/while loop specifies the condition, but runs once before checking it: do { /* ... */ } while (condition); A for loop compacts all loop control code into a single line: for (int i = 0; i < 10; i++) { /* .
Switches
22 Aug 2021
Switches The Crash Course Switches are an alternative to if statements that are part of a long chain of if-else if-else ifs. Switch statement: switch (variable) { case 0: statement; statement; break; case 1: statement; break; default: statement; break; } Switch expression: variable switch { 0 => "value", 1 => "value2", 2 => "another" _ => "default" } Introduction While if statements are the main decision making tool, there’s a second tool called a switch that can be pretty useful as well.
Advanced Math
21 Aug 2021
Advanced Math The Crash Course Math with integers an math with floating-point types have a lot of important differences. Integer division sheds any fractional part left over: 7 / 2 is 3. Floating-point division keeps the fractional part: 7.0 / 2.0 is 3.5. Each numeric type has a MinValue and MaxValue property that tell you what the largest and smallest representable numbers are for the type. Each floating-point type also defines PositiveInfinity and NegativeInfinity, as well as a special NaN (not-a-number) value.
User Input
21 Aug 2021
User Input The Crash Course Get input from the user with Console.ReadLine(); Convert it to what you need using various Convert.To____(_____); commands. This tutorial also contains a complete sample that determines the surface area and volume of a cylinder, with dimensions from the user. Introduction While these tutorials have covered a lot of ground, we have not learned anything that would allow us to interact with a user to get information.
Basic Math
20 Aug 2021
Basic Math The Crash Course Arithmetic in C# is very similar to virtually every other programming language out there, particularly C++, C, and Java. Addition works like this: int a = 3 + 4; Subtraction works like this: int b = 7 - 2; Multiplication uses the asterisk character (’*’) like this: int c = 4 * 3; Division uses the forward slash character (’/’) like this: int d = 21 / 7; The modulus operator (’%’) gets the remainder of a division operation like this: int e = 22 % 7; (In this case, the variable e will be 1.
Variables
20 Aug 2021
Variables The Crash Course Variables are used to store data. Each variable has a type, a name, and a current value. Declare variables with something like int x;. Assign values to variables with =: x = 3; You can reference a variable by name in an expression to retrieve the current value in the variable: Console.WriteLine(x); There are lots of types to choose from. The basic, fundamental set is: int, short, and long store positive and negative integers of different sizes.
Comments
15 Aug 2021
Comments Crash Course Comments are ways for you to add text for other people (and yourself) to read that the computer ignores. Comments are made by putting two slashes (//) in front of the text. Multi-line comments can also be made by surrounding it with asterisks and slashes like this: /* this is a comment */ Introduction In this short tutorial, we’ll cover the basics of comments. We’ll look at what they are, why you should use them, and how to do them (in three different ways).
Hello World: Your First C# Program
15 Aug 2021
Hello World: Your First C# Program Crash Course Start a new C# Console Application by clicking Create a new project on the launch page or going to File > New Project…, choosing the Console Application template, and giving your project a name. Compile and run your program with F5 or Ctrl + F5 Introduction In this tutorial, we’ll make our very first C# program. Our first program needs to be one that simply prints out some variation of “Hello World!
Installing Visual Studio
15 Aug 2021
Installing Visual Studio The Crash Course To program in C#, we will need a program that allows us to write C# code and run it. That program is Microsoft Visual Studio 2019 Community Edition. It can be downloaded and installed like you’d expect. Be sure to include the .NET Core cross-platform development workload when you install. Introduction Before we can begin writing C# code, we will need to get some tools to help us make programming easy.
Introduction
14 Aug 2021
Introduction Why Learn to Program? I can’t count the number of times I’ve talked to someone who wants to be a video or computer game programmer, and the following conversation ensues: Them: I’ve got this fantastic game idea that I want to make. It’s going to change gaming forever, and will make me rich! I just don’t know where to start. How do I start programming games? Me: How much programming do you already know?