Skip to content

Instantly share code, notes, and snippets.

@fatihdgn
Last active December 24, 2018 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fatihdgn/39309f25003270db4dc836cfd719433d to your computer and use it in GitHub Desktop.
Save fatihdgn/39309f25003270db4dc836cfd719433d to your computer and use it in GitHub Desktop.
Learn C# In 15 Minutes
// 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment