Learning C# in 15 Minutes

 In Cloud Software Development, Genel

I’ve come across these types of content on the internet over the years and always wanted to make my own, so here it is; I’m here to teach you C# in 15 minutes. Of course, you have to have a history with any type of OOP language (Java, C++ etc.) in general to get all this and make connections with your existing know-how. The whole purpose is to give you a cheatsheet to look at on your journey. If you believe you can learn any programming language from scratch in 15 minutes then your best bet would be to teach yourself C++ in 21 days

Alright then, let’s begin…

// With two slashes you can start a one-line comment.
/*
With slash-star you can start a multi-line comment and end it with the star-slash combo.
*/
// Variables
int i = 10; // You can declare variables with this format; <type> <name> = <value>;
// = used for assignment.
string s = "Hello world"; // Strings can be declared using double quotes.
double d = 42.05; // You can use float, decimal and double to represent floating-point numbers.
char c = 'q'; // You can use single-quotes to store characters.
string s2 = null; // Reference type values can be null, be aware!
bool isGood = true; // You can store boolean values using bool type.
var v = 100; // You can use the "var" keyword to omit the type.
// Clauses
if(i == 10) // == means equals
{
i = 20;
}
else if(i != 15) // You can use "else if" when your if condition fails. != means 'not' equals by the way.
{
}
else // And at last, the else clause when your every condition fails.
{
}
while(i < 50) // This is what the while condition looks like.
{
i++; // C# has the "++" too.
}
for(int x = 0; x < 50; x++) // The classic for loop. Nothing is out of the ordinary here.
{
i += x; // It's like "i = i + x;" but shorter. You can do "-=","*=" and "/=" too if you want to do your math like that.
}
string s3 = i == 50 ? "It's 50" : "It's not 50"; // We have ternary operator too...
// Arrays
string[] sArray = new string[10]; // You can create an array with a length of 10 like this.
sArray[0] = s; // Indexes starts from zero, just like it should!
sArray[1] = s2;
sArray[2] = s3;
// Methods
// You can declare methods like this; <type> <name>(|if any|<parameter type> <parameter name>)
void SayHi() // You can use the "void" as the type if you don't want to return anything.
{
Console.WriteLine("Hi");
}
int Add(int x, int y)
{
return x + y;
}
// And you can call them like this.
SayHi();
int i2 = Add(10,20);
// Classes
// You can declare classes like this.
/*
class <class name>
{
|if any|<property type> <property name>;
}
*/
class Person
{
string Name;
string Surname;
int Age;
string FullName() // You can declare methods in your classes like this.
{
return Name + " " + Surname; // You can do string concatenation.
}
}
// You can create an instance of your class like this;
Person p = new Person();
p.Name = "Fatih"; // You can reach the properties and methods inside your class using dot notation.
p.Surname = "DoÄŸan";
p.Age = 25;
string fd = p.FullName();
// Lists and key value pairs
using System.Collections.Generic; // If you want to store your objects as a list but want more flexible "thing" from arrays, you can use the generic List class from the "System.Collections.Generic" namespace. You can reference the namespace with the help of "using" keyword.
List<Person> pList = new List<Person>(); // Because this class is generic, it must include the type in the declaration. You can give it any type you want. It can be a class you declared too. Of course not every type is acceptable and you can filter the types you want when you are declaring a generic class, but let's keep it simple shall we?
pList.Add(p); // You can add an object to a list like this.
pList.Remove(p); // And you can remove one like this.
Dictionary<int, string> iDict = new Dictionary<int, string>(); // You can use Dictionary class to keep your key value pairs. This class is a generic class too and utilizes two seperate types to represent the key and value types. In this case, the key is integer and the value is a string type.
iDict[1] = "one"; // In this line, we are assigning the value "one" to a key of 1.

Let’s end it there. Please comment if you want me to include something that I haven’t yet.
Hope you enjoyed this, see you next time.

 

Önerilen Yazılar
İletişim

Start typing and press Enter to search