Definition
A definition is code that establishes what something is, such as a variable, method or class
For example: “
int score =10;`
Creates a variable named score that stores an integer, starting at 10
Statically typed
C# is statically typed, meaning the compiler knows the type of each variable before the program runs.
int score = 10;
score = "hello"; // Error: text cannot be stored in an intCompiler catches the incompatible assignment before the program runs
Garbage Collected
.NET automatically frees memory used by objects your program can no longer access
int[] numbers = new int[] { 1, 2, 3 };
numbers = new int[] { 4, 5, 6 };In the first line, numbers refers to the first array { 1, 2, 3} the second line changes numbers to a new array { 4, 5, 6} The first array still exists in memory, but nothing in this example refers to it anymore. The program has no way to reach it, so the garbage collector can claim its memory. Helps manage memory automatically and prevents unused objects from taking up RAM indefinitely
Object Oriented
Organizing code around objects that combine: Data & Methods Data: what something knows or has Methods: actions it can perform
An enemy object in a game could have Data: health, position Methods: Move(), Attack()
For example, let’s give a zombie just one piece of information: its health.
class Zombie
{
public int Health = 20;
}
This class is a blueprint that says “A zombie has health, starting at 20”
Creating an instance of Zombie means creating an object using the blueprint above. That would look like,
Zombie zombie = new Zombie();new Zombie() creates a zombie object. The variable zombie lets us access it:
The following line will show us the health of the zombie object we created,
Console.WriteLine(zombie.Health); // Shows 20In a game, spawning a zombie would involve creating the object like above.
Another example,
class Player
{
public int Score = 10;
public void AddPoints()
{
Score += 5;
}
}Then, use the object with
Player player = new Player();
player.AddPoints();
Console.WriteLine(player.Score); // Displays 15
Imperative
Telling the computer what steps to perform, in order. For example,
int score = 0;
score = score + 10;
Console.WriteLine(score);
Declarative
Tell the computer what result you want; let it determine the steps For example,
SELECT Name FROM Players WHERE Score > 100;Functional
Organize code around functions that take inputs and produce results For example,
int AddFive(int number)
{
return number + 5;
}
Console.WriteLine(AddFive(10)); // Displays 15Event-driven
Writing code that runs when something happens, such as a button click or a key press
For example,
void OnButtonClick(object sender, EventArgs e)
{
Console.WriteLine("You clicked the button!");
}Once this method is connected to the button’s click event, clicking the button causes it to run
Dynamic Typing
means a variables type isn’t fixed ahead of time; types are checked while the program runs
score = 10
score = "hello"In Python, both lines are allowed: score first refers to a number, then to a text
In C#, an int variable cannot later hold text,
int score = 10;
score = "hello"; // Compilation errorWith dynamic , this is made possible as type checking happens while the program runs. The compiler leaves certain type checks for the .NET runtime to perform. If an operation isn’t valid for the type, you get an error while the app runs.
dynamic score = 10;
score = "hello"; // Allowed
Unmanaged Code
code that runs without the .NET runtime managing it
Example, a c# app calling a function built into Windows.
Console.Beep();The C# code above is managed. Behind the scenes, .NET calls windows code to produce the sound. The windows code is unmanaged -.NET doesn’t manage it’s execution or memory
Base Class Library (BCL) = A collection of ready-mage types and tools provided by .NET for common tasks.
For example:
Console.WriteLine()— displays text.Math.Sqrt()— calculates a square root.File.ReadAllText()— reads text from a file.List<int>— stores a list of integers.
It saves you from having to build these basic features yourself.
.NET SDK
Is the toolkit you install to build C# programs.
It includes:
- The compiler that translates your C# code.
- The .NET runtime needed to run your program.
- Tools to create, build, and run projects, such as
dotnet.
Your IDE uses the SDK when you build and run your code.
Key information
C# project is a combination of two things, source code + configuration . Configuration = insturctions you give to the computer to help it know how to compile or trnslate the c# code into binary instructions the computer can run. source code = instructions you write in C# for the computer to run.
C# source code files use = .cs extension Project configuration file uses = .csproj extension
Compiling = Translating code you write into another form that can be executed. In C#, the compiler usually translates your C# code into Intermediate Language (IL), stored in a DLL. .NET then handles running it.
Compiler takes c# code and project configuration and produces the final binary instructions that the computer can run directly. The result is either a .exe or .dll file, which the computer can run
Literals = a chunk of code that defines some specific value, precisely as written in your code.
For example,
int john = age;The above is NOT a literal, as age is a variable
Below is a an integer literal,
int john = 25;Calling 25 a literal distinguishes a value written directly in the code from a variable that holds a value.
Identifiers = is a name used to identify something in code, such as a variable, class or method.
Hierarchical Organization
Some code elements live inside of other code elements. They are members or children of their container.
Console.WriteLine("Hello!");WriteLine is a method that belongs to the Console class.
The dot (.) lets you access WriteLine within Console.
Classes & Methods
Classes
Classes are primarily composed of two things: (1) the data they need to do their job and (2) tasks they can perform.
Tasks come in the form of methods, WriteLine is an example.
Methods
Methods are a named, reusable block of code that you can request to run.
Method call
The act of asking a method to run
Namespaces
A namespace groups related types, such as classes, under a name.
System.Console.WriteLine("Hello!");The hierarchy is:
System → namespace
Console → class
WriteLine → method
Examples of other namespaces,
Some examples in .NET are:
System— basic features, includingConsoleandMath.System.IO— working with files and folders.System.Collections.Generic— collections, such asList<int>.System.Net.Http— sending requests to websites and web services.
You can also create your own namespaces to organize your classes.
The Base Class Library
