C# Introduction
C# Introduction
C# Introduction
C # Programming
• C# (pronounced "C-sharp") is an object-
oriented programming language from
Microsoft that aims to combine the
computing power of C++ with the
programming ease of Visual Basic. C# is based
on C++ and contains features similar to those
of Java.
Features of C#
• C# is a modern, general-purpose, object-oriented
programming language .
• It was developed by Microsoft.
• C# was developed by Anders Hejlsberg.
• It is component oriented.
• It is easy to learn.
• It is a structured language.
• It produces efficient programs.
• It can be compiled on a variety of computer platforms.
• It is a part of .Net Framework.
MAIN FEATURES OF C#
1. SIMPLE
• Pointers are missing in C#.
• Unsafe operations such as direct memory manipulation are not allowed.
• In C# there is no usage of "::" or "->" operators.
• Since it's on .NET, it inherits the features of automatic memory management and
garbage collection.
• Varying ranges of the primitive types like Integer, Floats etc.
• Integer values of 0 and 1 are no longer accepted as boolean values.Boolean values
are pure true or false values in C# so no more errors of "="operator and
"=="operator.
• "==" is used for comparison operation and "=" is used for assignment operation.
2. MODERN
• C# has been based according to the current trend and is very powerful and simple
for building interoperable, scable, robust applications. C# includes built in support
to turn any component into a web service that can be invoked over the internet
from any application running on any platform.
3. OBJECT ORIENTED
• C# supports Data Encapsulation, inheritance, polymorphism, interfaces.
• (int,float, double) are not objects in java but C# has introduces structures(structs)
which enable the primitive types to become objects.int i=1;
string a=i Tostring(); //conversion (or) Boxing
The Common Language Infrastructure supports a two-
step compilation process
• Compilation
– The C# compiler: Translation of C# source to CIL
– Produces .dll and .exe files
– Just in time compilation: Translation of CIL to machine
code
• Execution
– With interleaved Just in Time compilation
– On Mono: Explicit activation of the interpreter
– On Window: Transparent activation of the interpreter
4. TYPE SAFE
• In C# we cannot perform unsafe casts like convert double to a boolean.Value types
(priitive types) are initialized to zeros and reference types (objects and classes) are
initialized to null by the compiler automatically.arrays are zero base indexed and
are bound checked.Overflow of types can be checked.
5. INTEROPERABILITY
• C# includes native support for the COM and windows based applications.
• Allowing restriced use of native pointers.
• Users no longer have to explicitly implement the unknown and other COM
interfacers, those features are built in.
• C# allows the users to use pointers as unsafe code blocks to manipulate your old
code.
• Components from VB NET and other managed code languages and directlyt be
used in C#.
6. SCALABLE AND UPDATEABLE
• .NET has introduced assemblies which are self describing by means of their
manifest. manifest establishes the assembly identity, version, culture and digital
signature etc. Assemblies need not to be register anywhere.To scale our
application we delete the old files and updating them with new ones. No
registering of dynamic linking library.Updating software components is an error
prone task. Revisions made to the code. can effect the existing program C# support
versioning in the language. Native support for interfaces and method overriding
enable complex frame works to be developed and evolved over time.
Code Execution Process
Compiler time process.
namespace namespace_name
{
// code declarations
}
• To call the namespace
namespace_name.item_name;
using System;
namespace first_space
{ class namespace_cl {
public void func()
{ Console.WriteLine("Inside first_space");
}}}
namespace second_space
{ class namespace_cl {
public void func()
{ Console.WriteLine("Inside second_space");
}}}
class TestClass
{ static void Main(string[] args)
{ first_space.namespace_cl fc = new first_space.namespace_cl();
second_space.namespace_cl sc = new second_space.namespace_cl();
fc.func();
sc.func();
Console.ReadKey(); } }
Output:
Inside first_space
Inside second_space
using System;
Operating System
A Common Language
Specification (CLS)
• A Common Language Specification (CLS) is a
document that says how computer
programs can be turned into MSIL code. When
several languages use the same bytecode,
different parts of a program can be written in
different languages. Microsoftuses a Common
Language Specification for their .NET
Framework.
The Base Class Library (BCL)
• The Base Class Library (BCL) is literally that,
thebase. It contains basic, fundamental types
like System.String and System.DateTime .
TheFramework Class Library (FCL) is the
wider librarythat contains the
totality: ASP.NET, WinForms, the XML stack,
ADO.NET and more. You could say that the FCL
includes the BCL.
WPF
• WPF : Windows Presentation Foundation
(or WPF) is a graphical subsystem by Microsoft
for rendering user interfaces in Windows-
based applications. WPF, previously known as
"Avalon", was initially released as part of .NET
Framework 3.0 in 2006.
LINQ
• LINQ (Language Integrated Query) allows
writing queries even without the
knowledge of query languages like SQL,
XML etc. LINQ queries can be written for
diverse data types.
WCF
• WCF stands for Windows Communication
Foundation. It is a framework for building,
configuring, and deploying network-
distributed services. Earlier known as Indigo, it
enables hosting services in any type of
operating system process.
ADO.NET
• ADO.NET(ActiveX Data Objects ) is a data
access technology from the Microsoft
.NETFramework that provides communication
between relational and non-relational systems
through a common set of
components.ADO.NET is a set of computer
software components that programmers can
use to access data and data services from the
database.
ASP.NET
• ASP.NET is a web application framework
developed and marketed by Microsoft to
allow programmers to build dynamic web
sites. It allows you to use a full featured
programming language such as C# or VB.NET
to build web applications easily.
• You can see Operating System at the base. It
can be any Microsoft windows platform that
support Visual Studio .NET.
/*
* C# Program to Find Roots of a Quadrati
c Equation
*/
using System;
namespace example }
{ public void compute()
class Quadraticroots {
{ int m;
double a, b, c; double r1, r2, d1;
d1 = b * b - 4 * a * c;
public void read() if (a == 0)
{ m = 1;
Console.WriteLine(" \n To find the roots o else if (d1 > 0)
f a quadratic equation of the form a*x*x + b m = 2;
*x + c = 0"); else if (d1 == 0)
Console.Write("\n Enter value for a : "); m = 3;
a = double.Parse(Console.ReadLine()); else
Console.Write("\n Enter value for b : "); m = 4;
b = double.Parse(Console.ReadLine());
Console.Write("\n Enter value for c : ");
c = double.Parse(Console.ReadLine());
switch (m) ”+”+” i” + r2);
{ Console.WriteLine("\n First root is “+r1+”-”+” i” + r
case 1: Console.WriteLine("\n Not a Quadr 2);
atic equation, Linear equation");
Console.ReadLine(); Console.ReadLine();
break; break;
case 2: Console.WriteLine("\n Roots are Re }
al and Distinct"); }
r1 = (-b + Math.Sqrt(d1)) / (2 * a); }
r2 = (-b - Math.Sqrt(d1)) / (2 * a);
Console.WriteLine("\n First root is “+ r1); class Roots
Console.WriteLine("\n Second root is “+ r2); {
Console.ReadLine(); public static void Main()
break; {
case 3: Console.WriteLine("\n Roots are Re Quadraticroots qr = new Quadraticroots();
al and Equal"); qr.read();
r1 = r2 = (-b) / (2 * a); qr.compute();
Console.WriteLine("\n First root “+r1); } }}
Console.WriteLine("\n Second root “+r2);
Console.ReadLine();
break;
case 4: Console.WriteLine("\n Roots are Im
aginary");
r1 = (-b) / (2 * a);
r2 = Math.Sqrt(-d1) / (2 * a);
Console.WriteLine("\n First root is “+r1+
• In C#, these data types are categorized based
on how they store their value in the memory.
C# includes following categories of data types:
• Value type
• Reference type
Value Type and a Reference Type
Value Type:
// s is compiled as a string
var s = "Hello";
// a is compiled as int[]
var a = new[] { 0, 1, 2 };
// boxing
object obj = num;