Introduction to C#
What is C#
C#(pronounced as see sharp) is a multi-paradigm programming language encompassing strong typing, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines. It was developed by Microsoft within its .NET initiative and later approved as a standard by Ecma (ECMA-334) and ISO (ISO/IEC 23270:2006). C# is one of the programming languages designed for the Common Language Infrastructure.History of C#
- During the development of the .NET Framework, the class libraries were originally written using a managed code compiler system called Simple Managed C (SMC).
- C#'s principal designer and lead architect at Microsoft is Anders Hejlsberg, who was previously involved with the design of Turbo Pascal, Embarcadero Delphi.
- In January 1999, Anders Hejlsberg formed a team to build a new language at the time called Cool, which stood for "C-like Object Oriented Language".
- Microsoft had considered keeping the name "Cool" as the final name of the language, but chose not to do so for trademark reasons. By the time the .NET project was publicly announced at the July 2000 Professional Developers Conference, the language had been renamed C#, and the class libraries and ASP.NET run time had been ported to C#.
Name
The name "C sharp" was inspired by musical notation where a sharp indicates that the written note should be made a semitone higher in pitch. This is similar to the language name of C++, where "++" indicates that a variable should be incremented by 1. The sharp symbol also resembles a ligature of four "+" symbols (in a two-by-two grid), further implying that the language is an increment of C++.
Version
The most recent version is C# 6.0, which was released on July 20, 2015.
Features of C# versions
Features of C# versions
C# 1.0
Microsoft released the first version of C# with Visual Studio 2002. Use of Managed Code was introduced with this version. C# 1.0 was the first language that developer adopted to build .NET applications.
C# 2.0
Generics
Partial types
Anonymous methods
Iterators
Nullable types
Getter/setter separate accessibility
Method group conversions (delegates)
Co- and Contra-variance for delegates
Static classes
Delegate inference
Microsoft released the first version of C# with Visual Studio 2002. Use of Managed Code was introduced with this version. C# 1.0 was the first language that developer adopted to build .NET applications.
C# 2.0
Generics
Partial types
Anonymous methods
Iterators
Nullable types
Getter/setter separate accessibility
Method group conversions (delegates)
Co- and Contra-variance for delegates
Static classes
Delegate inference
C# 3.0
Implicitly typed local variables
Object and collection initializers
Auto-Implemented properties
Anonymous types
Extension methods
Query expressions
Lambda expressions
Expression trees
Partial methods
C# 4.0
Dynamic binding
Named and optional arguments
Tuples
Tuples
Generic co- and contravariance
Embedded interop types ("NoPIA")
C# 5.0
Asynchronous methods
Caller info attributes
C# 6.0
Compiler-as-a-service (Roslyn)
Import of static type members into namespace
Exception filters
Await in catch/finally blocks
Auto property initializers
Default values for getter-only properties
Expression-bodied members
Null propagator (null-conditional operator, succinct null checking)
String Interpolation
name of operator
Dictionary initializer
C# 7.0 (proposals)
Local functions
Pattern matching
Records / algebraic data types
Nullability tracking
Async streams and disposal
Strongly typed access to wire formats
In this course we are going to use c# 6.0 as standard, .NetFramework 4.6 as a framework and VisualStudio2015/VisualStudio2015Community(FreeVersion) as a tool.
Write your first C# code
We want to print - "Hello!!! C# coders" in console.To print - "Hello!!! C# coders...." we have follow the below steps
step 1 - download and install VisualStudio2015/VisualStudio2015Community
to dowload VisualStudio2015Community - click here
step 2 - open VisualStudio2015Community
step 3 - create a console application using project by selecting File -> New -> Project
step 4 - choose Console Application
step 5 - now in the VisualStudio IDE, write the code to print "Hello!!! C# coders"
step 6 - write the below code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello!!! C# coders....");
}
}
}
Output : Hello!!! C# coders....
Congratulations!! You have written your first C# code.
Object Oriented Programming concept
What is procedural and object oriented programming?
Procedural Programming - Procedural programming technique is a programming technique where the flow of the entire program goes from the top to bottom step by step and uses various functions to perform certain tasks. The name of this technique has been derived from procedure call(function call/subroutine call) in structured programming. Its a top down approach to programming. Example of procedural programming - BASIC, C, FORTRAN, and Pascal.
Object Oriented Programming - Object oriented programming technique is a programming technique where objects interact with each other to perform tasks or exchange data using fields in the form of attributes and procedures in the form of methods. Its a bottom up approach to programming. Example of object oriented programming techniques - JAVA, PHP, Ruby, C#.
What are the differences between procedural and object oriented programming?
Object Oriented Programming
|
Procedural Programming
|
Program is divided into objects.
|
Program is divided into functions
|
Bottom-Up approach
|
Need member methods to to access data
|
Use access specifier
|
Doesn't have access specifier
|
Over loading and over ridding is
possible
|
Doesn't support over loading and over ridding
|
Code is reusable and extendable
|
Code is neither reusable nor extendable
|
What are the four main pillars of object oriented programming?
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Abstraction - Abstraction means to represent essential features with out showing the details of the background.
Suppose we have 4 functions (a) Add, (b) Sub, (c)Mul, (d) Div
Purpose of Add is to add two numbers and show the result
int Add(int a, int b)
{
logic for Add...
}
Purpose of Sub is to subtract two numbers and show the result
int Sub(int a, int b)
{
logic for Sub...
}
Purpose of Mul is to multiply two numbers and show the result
int Mul(int a, int b)
{
logic for Mul...
}
Purpose of Div is to divide two numbers and show the result
int Div(int a, int b)
{
logic for Div...
}
Here we know the purpose of all the four functions but we don't know the logic written behind each functions. This is a simple example of how in real life coding we implement abstraction.
Encapsulation - Encapsulation is the technique that binds the data along with the code to keep them safe and away from outside influence.
Lets take a simple example of a class Bike, in this class we have attributes and methods that defines Bike.
public class Bike()
{
private int speed;
private int oilConsuption;
private int totalDistanceCovered;
private float mileage;
mileage = (float)(oilConsuption/totalDistanceCovered);
public float Mileage()
{
return(mileage);
}
}
In this example we have seen that there are four attributes and one method. The attributes are private and we can't access it from outside the class Bike. We want to use this class to find out the mileage of the Bike. We can achieve this by calling the method Mileage from outside but we can't access the private variables. Here we can use and reuse the class Bike to find out the desired result but we can't manipulate the data inside the class.
Inheritance - Inheritance is the technique using which we can extend code with out having to redefine the code. Inheritance defines the reusability of a code and enhances efficiency. In order to use Inheritance we need a parent class and we can extend the features of the existing class by deriving it in a child class.
We can use the already existing class Bike and introduce few features in the child class.
public class Yamaha:Bike
{
private string colour = "White";
public string Colour()
{
return (color);
}
}
Here we have used the Parent class Bike and extended its functionalities by adding method Colour and attribute colour. The new child class Yamaha will inherit all the features of Bike class and along with that it will have the new feature which shows the colour of the Bike as well.
Polymorphism - As the name suggest polymorphism means the existence of an object in various form such that both exists distinctly.
We can use an example of a method Distance() which returns the distance between two points.
public class measure
{
private int distance1;
private float distance2;
public int Distance()
{
return(distance1);
}
public float Distance()
{
return(distance2);
}
}
Here in this example we have two different methods Distance both have same structure yet they are different, the way we distinguish the two by the return type. This is a very good example of how polymorphism works.
We will revisit all these OOP concepts again during our course







