Csharp
C# (pronounced "C sharp") is a powerful and modern programming language developed by Microsoft. It is widely used for building a variety of applications, including desktop software, web applications, game development, and mobile apps. C# combines the features of C++ and the simplicity of Visual Basic, resulting in a language that is both robust and developer-friendly. It has a comprehensive standard library, called the .NET Framework, which provides a rich set of tools and APIs for developing applications across different platforms.
Module 1: C# Basics
Introduction to C#
C# is a general-purpose, modern and object-oriented programming language pronounced as 'C Sharp'. It was developed by Microsoft led by Anders Hejlsberg and his team within the .NET initiative and was approved by the European Computer Manufacturers Association (ECMA) and International Standards Organization (ISO).
// A Hello World! program in C#. using System; namespace HelloWorld { class Hello { static void Main() { Console.WriteLine('Hello World!'); Console.ReadKey(); } } }
Variables and Data Types
C# is a statically-typed language, which means that variables must be declared before they're used. Some of the most common data types are int (integer), double (decimal number), char (character), and string (sequence of characters).
int number = 10; double rate = 3.14; char letter = 'a'; string phrase = 'Hello, world!';
Operators
C# includes a comprehensive set of operators, which are symbols that specify which operations to perform in an expression. C# supports a wide range of operators including Arithmetic Operators, Comparison Operators, Logical Operators, and Bitwise Operators.
int x = 10; int y = 5; // Arithmetic operators int add = x + y; // addition int sub = x - y; // subtraction int mult = x * y; // multiplication int div = x / y; // division // Comparison operators bool equal = x == y; // equal to bool notEqual = x != y; // not equal to
Control Structures
Control structures allow you to specify the flow of your program's execution. This includes conditional statements like if and switch, and loops like for, while, and do-while.
// if statement if (x > y) { Console.WriteLine('x is greater than y'); } // for loop for (int i = 0; i < 10; i++) { Console.WriteLine(i); }
Module 2: Object-Oriented Programming in C#
Classes and Objects
Classes and objects are the two main aspects of object-oriented programming. A class is a blueprint for the object. We can think of a class as a sketch (prototype) of a house. An object is an instance of a class.
public class Car { string color; public Car(string color) { this.color = color; } }
Inheritance
Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in C# by which one class is allowed to inherit the features(fields and methods) of another class.
public class Vehicle { string brand = 'Ford'; } public class Car : Vehicle { string model = 'Mustang'; }
Polymorphism
Polymorphism allows a child class to inherit the methods and fields of its parent class. It also allows the child class to override the methods of its parent class.
public class Animal { public virtual void animalSound() { Console.WriteLine('The animal makes a sound'); } } public class Pig : Animal { public override void animalSound() { Console.WriteLine('The pig says: wee wee'); } }
Encapsulation
Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates.
public class Employee { private string name; // private variable public string Name // public method to access and modify the private variable { get { return name; } set { name = value; } } }
Module 3: Advanced Concepts in C#
Generics
Generics allow you to define type-safe data structures, without committing to actual data types. This leads to a high level of code reuse, type safety and performance.
public class MyGenericClass<T> { private T genericMemberVariable; public MyGenericClass(T value) { genericMemberVariable = value; } public T genericMethod(T genericParameter) { Console.WriteLine('Parameter type: {0}, value: {1}', typeof(T).ToString(), genericParameter); Console.WriteLine('Return type: {0}, value: {1}', typeof(T).ToString(), genericMemberVariable); return genericMemberVariable; } }
Exception Handling
An exception is a problem that arises during the execution of a program. Exception handling in C# is built upon four keywords: try, catch, finally, and throw.
try { int[] arr = new int[5]; arr[6] = 10; // Exception is thrown here } catch (IndexOutOfRangeException e) { Console.WriteLine('An exception occurred: ' + e.Message); } finally { Console.WriteLine('This block is always executed'); }
File I/O
C# provides inbuilt classes to handle file I/O operations. The System.IO namespace contains classes that perform operations on files, directories, and paths; create, delete, or manipulate files etc.
using System.IO; string fileName = 'test.txt'; // Write to a file File.WriteAllText(fileName, 'Hello, world!'); // Read from a file string content = File.ReadAllText(fileName); Console.WriteLine(content);
Delegates and Events
A delegate is a type that represents references to methods with a particular parameter list and return type. Events are a way that a class can notify its clients when something happens.
public delegate void MyDelegate(string msg); // declare a delegate // set target method MyDelegate del = new MyDelegate(Hello); del('Hello, World!'); // can now call method static void Hello(string strMessage) { Console.WriteLine(strMessage); }
Module 4: Working with .NET and ASP.NET
.NET Framework Basics
.NET is a developer platform made up of tools, programming languages, and libraries for building many different types of applications. ASP.NET extends the .NET platform with tools and libraries specifically for building web applications.
using System; using System.Windows.Forms; public class HelloWorld : Form { static void Main() { Application.Run(new HelloWorld()); } HelloWorld() { Button button = new Button(); button.Text = 'Hello World'; button.Click += new EventHandler(button_Click); Controls.Add(button); } void button_Click(object sender, EventArgs e) { MessageBox.Show('Button clicked'); } }
ASP.NET Web Applications
ASP.NET is an open-source, server-side web-application framework designed for web development to produce dynamic web pages. It allows you to use a full featured programming language such as C# to build web applications easily.
using System; using System.Web; using System.Web.UI; public class HelloWorld : Page { protected void Page_Load(object sender, EventArgs e) { Response.Write('Hello, World!'); } }
Entity Framework
Entity Framework (EF) is an open source object-relational mapping (ORM) framework for ADO.NET, part of .NET Framework. It enables developers to work with data in the form of domain-specific objects and properties, such as customers and customer addresses, without having to concern themselves with the underlying database tables and columns where this data is stored.
using (var context = new BloggingContext()) { var blog = new Blog { Url = 'http://sample.com' }; context.Blogs.Add(blog); context.SaveChanges(); }
LINQ
Language Integrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages.
int[] scores = { 90, 71, 82, 93, 75, 82 }; IEnumerable<int> scoreQuery = from score in scores where score > 80 select score; foreach (int i in scoreQuery) { Console.Write(i + ' '); }