Skip to main content

C# With Dean Laganiere

Posted in

C#

Language developed by MIcrosoft in 2000
Part of the .NET Framework

Mostly the same data types as C++
Very similar language

Using = #include
namespace is where all of your classes in the files go
Class "program" contains your main function
Console.WriteLine("String here"); is equivalent to cout << "string here"; 
Define varibales like: datatype nameofvariable = initialvalueifyouwant
Variable = Console.ReadLine() is the way you input
int is actually an object not just a data type so it has functions
Convert strings into ints by yourint = int.Parse(yourstring)

For loops work the same as C++
Declare arrays like: int[] nums = new int[10];
C# also has "foreach loop" it works like foreach (int num in nums) so it will loop through the array nums[]

Classes vs Struct
In C# everything is private by default
Structs are immutable

Interfaces
They're like abstract classes in C++
All interfaces should start with an I
When a class inherits an interface you have to define the functions in the interface
Interfaces can't have members

In  C# you can define default values for parameters that don't get passed into a function

Lists
Works a lot like a vector in C++
More powerful than an array
Declare like: List<Typehere> namehere = new List<typehereagain>(); // put them in arrow brackets literally
.OrderBy can be used to sort a list really easily. 

File I/O
Use the StreamReader class
Declare Like: StreamReader reader = new StremReader(@"FilePath"); 
.ReadLine() will read a line
.Read() will read the next character
.Peek() will read the next character but not actually consume it and move the position forward