Introduction to C#
C# is a high-level, general-purpose, compiled programming language.
History
It was developed and is maintained by Microsoft.
C# Resources
- Getting Started: Language Download
- Getting Started: Development Tools
- Getting Started: Testing
- Getting Started: Deployment
- Beginner Tutorials
- Intermediate Tutorials
- Advanced Tutorials
- Good Beginner Blog
- Good Intermediate Blog
- Good Advanced Blog
- Official Documentation
Quick Reference – C#
Here is a quick reference for the basics of C#…
Hello World in C#
using System; public class Program { public static void Main() { Console.WriteLine("What is your name?"); string yourName = Console.ReadLine(); Console.WriteLine("Hello " + yourName + "!"); } }
C#: Basic Syntax
Variables & Data Types
bool (true/false) byte (8 bit) int (32 bit) uint short (16 bit) ushort long (64 bit) ulong float (32 bit) double (64 bit) decimal (precise decimal with 29 significant digits) char (single unicode character) string DateTime object
Branching
if (condition) { // do something } else { // do something else }
Arrays
int[] primeNumbers = { 2, 3, 5, 7, 11 };
Lists
List primeNumbers = new List {2, 3, 5, 7, 13};
Dictionaries
Dictionary<string, string> dictionaryName = new Dictionary<string, string>();
Loops
You can loop through all the items in an array:
string[] myList = {"one", "two", "three", "four", "five"}; foreach (string item in myList) { Console.WriteLine(item); }
You can loop a fixed number of times:
for (int i = 0; i < 5; i++) { Console.WriteLine(i); }
You can loop while a condition is true:
while (true) { // do something that may change the condition }
You can loop until a condition is false (always loops once):
do { // do something that may change the condition } while (true);
C#: Object-Oriented Syntax
Classes
class Animal { public string Species; public string Name; public string Sound; }
Objects
Animal myPet = new Animal(); myPet.Species = "dog"; myPet.Name = "Fido"; myPet.Sound = "Bow-wow";
Methods
class MyCalculator { public int Add(int num1, int num2) { return num1 + num2; } }
Constructors
class Animal { public string Species; public string Name; public string Sound; public Animal(string species, string name, string sound) { Species = species; Name = name; Sound = sound; } } Animal myPet = new Animal("dog", "Fido", "Bow-wow"); Animal yourPet = new Animal("cat", "Garfield", "Screech! Hiss!");