
This course includes our updated coding exercises so you can practice your skills as you learn.
See a demo
This article outlines why students should learn C#, emphasizing its cross-platform versatility, industry demand, and applications in web, mobile, and game development.
Do you want to learn an easy, yet powerful and awesome programming language? Then you should learn C#!
"Hello World" is the first application that we are going to build to get started with C#.
C# is an object-oriented programming language. In Object-Oriented Programming methodology, a program consists of various objects that interact with each other by means of actions.
The data type tells a C# compiler what kind of value a variable can hold. C# includes many in-built data types for different kinds of data, e.g.,
String
number
float
char
bool
etc...
In C#, the data types are categorized based on how they store their value in the memory. C# includes two categories of data types:
Value type
Reference type
Type conversion which is also known as type casting is the process of converting one type of data to another one...
A variable is nothing but just a name given to a storage area that our programs can manipulate...
A constant refers to a fixed value that the application cannot change during its execution. The constants are treated just like regular viables except that their values cannot be modified after their definition...
Operators are used to manipulate variables and values in a program. C# supports a number of operators that are classified based on the type of operations they perform.
Decision statements are used to control the flow of execution of the code. These statements are used to execute particular piece of code if or if not a certain condition is met. There are four major types of decision statements in C#:
If statement
If/else statement
Else if statements
Switch statement
If you want to execute your code repeatedly certain number of times or until a certain condition is met, you can use C# iterative statements which are also known as loops.
There are three types of loops in C#:
For loop
While loop
Do-While loop
Access modifiers are used to specify the level at which each variable, function or object can be accessed within the application.
In C#, there are five types of access modifiers which are as follows:
public
private
protected
internal
protected internal
A function/method allows you to encapsulate a piece of code and call it from any part of your application...
A struct is a value type so it is faster than a class object. Use struct whenever you want to just store the data. Generally structs are good for game programming. However, it is easier to transfer a class object than a struct. The C# struct is a lightweight alternative to a class. It can do almost the same as a class, but it's less "expensive" to use a struct rather than a class.
In C#, enum is a value type data type. The enum is used to give a name to each constant so that the constant integer can be referred using its name..
A String is immutable, meaning String cannot be changed once created. And each append causes a string copy. With StringBuilder we eliminate this copy....
An array is a special type of data type which can store fixed number of values sequentially. An array is used to store a collection of data of the same type, but it is often more useful to think of an array as a collection of variables of the same type...
In C# a class is like a blueprint of a specific object. In the Real World every object has a color, it has a shape and it has functionalities. Ex: A Person has the Age, Height, Birthday, Profession etc. The same way in Object Oriented Programming (OOP) a class defines certain properties, fields, events, methods etc....
What is Inheritance?
Inheritance in object oriented programming is used to make applications faster and simpler.
With inheritance, we build several types based on a single abstraction. It defines which is the base/parent class and which is the child one. A child class is a class that inherits from another one. And the class that other classes inherit from is the base class.
In C# the ultimate base class is the object class.
What is Polymorphism?
The word polymorphism means having many forms. And in C# is a concept where a method can be defined more than one time.
There are two Types of Polymorphism:
Method Overloading
Method Overriding
Check out this tutorial to learn more...
Interfaces in C# are a means to get around the lack of multiple inheritances in C#, meaning you cannot inherit from multiple classes but you can implement multiple interfaces. Interfaces make programs more compact and easier to maintain. This extra complexity leads to greater simplicity...
Collections are specialized classes for data storage and retrieval.
There are two types of collections available in C#:
1. Non-generic collections
2. #Generic collections.
Every collection class implements the IEnumerable interface so values from the collection can be accessed using a foreach loop....
ArrayList is a non-generic type of collection in C#. It can contain elements of any data types. It is similar to an array, except that it grows automatically as you add items in it.
Unlike an array, you don't need to specify the size of ArrayList...
The SortedList collection stores key-value pairs in the ascending order of key. SortedList class implements IDictionary & ICollection interfaces, so elements can be accessed both by key and index. SortedList key can be of any data type, but you cannot add keys of different data types in the same SortedList.
Internally, SortedList maintains two object[] array, one for keys and another for values...
Stack is a special type of collection which stores elements in LIFO style(Last In First Out). C# includes a generic and non-generic Stack. Here, we are going to learn about the non-generic stack.
Important notes:
- Stack stores the values in LIFO (Last in First out) style.
- Use the Push() method to add elements into Stack.
- The Pop() method returns and removes elements from the top of the Stack.
- The Peek() method always returns top most element in the Stack.
The Queue collection is the opposite of the Stack collection. It stores elements in FIFO (First In First Out) style.
We can use the Enqueue() method to add values and the Dequeue() method to retrieve the values from the Queue.
The Peek() method returns, but does not remove top most element.
Calling the Dequeue() & Peek() on an empty queue throws an exception.
The Hashtable Collection stores key-value pairs. Hashtable elements are key-value pairs stored in DictionaryEntry. So you cast each element in Hashtable to DictionaryEntry...
An Indexer is a special type of property that allows a class or structure to be accessed the same way as array for its internal collection. The indexed value can be set or retrieved without explicitly specifying a type or instance member.
To define an Indexer we need to:
Declare an array to store the data elements
Define the indexer to allow client code to use the [ ] notation
The indexers are usually known as smart arrays in C#....
When an error occurs, either CLR or program code throws an exception which contains necessary information about the error.
SystemException class is a base class for all the exceptions that can occurs during execution of the program
But, how can we handle exceptions?
C# provides built-in support to handle the exception using try, catch & finally block. You can also create your own custom exception classes...
We use delegates to point to different functions with same signature. Also, by using delegates we pass a functions as a parameter for another function.
A delegate can be seen as a placeholder for a/some method(s)....
.NET Team launches events for developers.
Microsoft is a publisher who launches (raises) an event and notifies the community
The community is the subscriber of the event and attends (handle) the event.
Events in C# follow a similar concept. An event has a publisher, subscriber, notification and a handler. Generally, UI controls use events extensively...
Generics are the most powerful feature of C#.
They were were introduced in C# 2.0 and allow you to define type-safe data structures. A generic class can be defined using angle brackets <>.
Why should you use Generics?
Code is not duplicated for multiple types of data...
Once we create a class we can not create a second one with the same name, unless they are in different namespaces. So, Microsoft decided to change this, with the introduction of something called partial classes.
This means that by using the partial classes we can create 2 classes with the same name within the same namespace.
With Partial Classes multiple developers can work simultaneously with a single class in separate files...
When using the static keyword it means that class cannot be instantiated using the new keyword. So, the properties, variables, methods etc can be accessed by directly using the classname instead of creating a new object.
Also, static classes are well known to be used to create extension methods...
In C# a value type cannot be assigned a null value.
For example, int i = null will give you a compile time error.
C# 2.0 introduced nullable types that allow you to assign null to value type variables...
Func is a generic delegate included in the System namespace. It has zero or more input parameters and one out parameter.
Func delegate:
Can have zero to 16 input parameters
Can be used with an anonymous method
Can be used with lambda expressions
Action is also a delegate type defined in the System namespace. An Action type delegate is the same as Func delegate except that the Action delegate doesn't return a value. In other words, an Action delegate can be used with a method that has a void return type.
Predicate delegate takes one input parameter and return a boolean - true or false.
On this course, you'll be introduced to C#, a language often celebrated as a hybrid that merges the robust, low-level capabilities of C and C++ with modern programming paradigms. C# is not just another language within the .NET framework; it has emerged as one of the most favored due to its simplicity, type safety, and seamless integration with the Microsoft ecosystem.
Throughout the course, you'll discover how C# can be leveraged for a multitude of applications. It's exceptionally powerful for creating Windows desktop applications, where it thrives in environments requiring high performance and graphical user interfaces. Beyond desktops, C# excels in game development, particularly with engines like Unity, making it a go-to choice for both indie developers and large studios.
Moreover, C# is not confined to Windows alone. It's increasingly utilized in web development, offering robust solutions for server-side programming, including frameworks like ASP.NET. The language's versatility extends to mobile development as well. With tools like Xamarin, C# developers can write cross-platform applications that run natively on iOS, Android, and beyond, without compromising on performance or user experience.
By the end of this course, you'll not only understand C#'s syntax and structure but also how to apply it in diverse scenarios, from traditional software to cutting-edge mobile and web technologies.