Common Language Infrastructure (CLI)

Download Report

Transcript Common Language Infrastructure (CLI)

Introduction to C#.net
Unit I
C# .NET
 Modern, general-purpose, object-oriented programming
language developed by Microsoft(Anders Hejlsberg and
team) and approved by European Computer Manufacturers
Association (ECMA) and ISO
 C# is designed for Common Language Infrastructure (CLI),
consists of the executable code and runtime environment
that allows use of various high-level languages on different
computer platforms and architectures
Strong Programming Features of C#












Boolean Conditions
Automatic Garbage Collection
Standard Library
Assembly Versioning
Properties and Events
Delegates and Events Management
Easy-to-use Generics
Indexers
Conditional Compilation
Simple Multithreading
LINQ and Lambda Expressions
Integration with Windows
The .Net Framework
platform that provides tools and technologies you need
to build
 Windows applications
 Web applications
 Web services
 Designed in such a way that it can be used from any of
the following languages: C#, C++, Visual Basic, Jscript,
COBOL, etc.
 All these languages can access the framework as well
as communicate with each other.
Contd..
 Provides the necessary compile time and run-time
foundation to build and run any language that conforms to
the Common Language Specification (CLS)
 components of .Net Framework
 Common Language Runtime (CLR)
 runtime environment of the .Net Framework , that
executes and manages all running code like a Virtual
Machine.
 .Net Framework Class Library (FCL).
 huge collection of language-independent and typesafe reusable classes.
 FCL are arranged into a logical grouping according to
their functionality and usability is called Namespaces.
How Microsoft .Net Framework works
Common Language Runtime (CLR)
 It works as a layer between Operating Systems and the
applications written in .Net languages that conforms to
the Common Language Specification (CLS).
 The main function of CLR is to convert the Managed
Code into native code and then execute the Program.
 The Managed Code compiled only when it needed, that
is it converts the appropriate instructions when each
function is called .
 CLR's Just In Time (JIT) compilation converts
Intermediate Language (MSIL) to native code on
demand at application run time.
Contd…
 During the execution of the program , CLR) manages memory,
Thread execution, Garbage Collection (GC) , Exception Handling,
Common Type System (CTS), code safety verifications, and other
system services.
 The CLR defines the Common Type System (CTS), which is a
standard type system used by all .Net languages .
 That means all .NET programming languages uses the same
representation for common Data Types , so Common Language
Runtime (CLR) is a language-independent runtime environment .
 CLR environment is also referred to as a managed environment,
because during the execution of a program it also controls the
interaction with the Operating System.
Contd…
 Microsoft .Net Languages Source Code are compiled
into Microsoft Intermediate Language (MSIL also called
as Intermediate Language (IL)) .
 MSIL is a CPU independent set of instructions that can
be converted to the native code.
 Metadata also created in the course of compile time
with MSIL and stored it with the compiled code .
 Metadata is completely self-describing .
 Metadata is stored in a file called Manifest, and it
contains information about the members, types,
references and all the other data that the CLR needs for
execution .
Contd...
 CLR uses metadata to locate and load classes, generate native
code, provide security, and execute Managed Code.
 Both MSIL and Metadata assembled together is known as
Portable Executable (PE) file.
 Portable Executable (PE) is supposed to be portable across all 32bit operating systems by Microsoft .Net Framework.
 During the runtime the CLR's Just In Time (JIT) compiler converts
MSIL code into native code to the Operating System.
 The native code is Operating System independent and this code
is known as Managed Code , that is, the language's functionality
is managed by the .NET Framework .
 CLR provides various Just In Time (JIT) compilers, and each
works on a different architecture depends on Operating
Systems, that means the same MSIL can be executed on
components of the .Net framework












Common Language Runtime (CLR)
The .Net Framework Class Library
Common Language Specification
Common Type System
Metadata and Assemblies
Windows Forms
ASP.Net and ASP.Net AJAX
ADO.Net
Windows Workflow Foundation (WF)
Windows Presentation Foundation
Windows Communication Foundation (WCF)
LINQ
CTS Datatypes
 Boolean, Byte, Char, DateTime, Decimal, Double,
Int16, Int32, Int64, SByte, Single, UInt16, UInt32, and
UInt64
IDE for C#
 Visual Studio 2010 (VS)
 Visual C# 2010 Express (VCE)
 Visual Web Developer
C# Programs on Linux or Mac OS
 Mono is an open-source version of the .NET
Framework which includes a C# compiler and
runs on several operating systems, including
various flavors of Linux and Mac OS, Android,
BSD, iOS, Linux, OS X, Windows, Solaris, and
UNIX.
Structure of C# program
Namespace declaration
A class
Class methods
Class attributes
A Main method
Statements and Expressions
Comments
Including namespaces using the “USING” keyword
The purpose of a using directive is to save you from typing the namespace
every single time you need to use a class.
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine(“Hello”);
}
}
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Hello”);
}
}
16
Accepting Values from User
 int num;
 num = Convert.ToInt32(Console.ReadLine());
 double = Double.Parse(Console.Readline());
Read Appropriate type






string input = Console.ReadLine();
double d;
if (!Double.TryParse(input, out d))
Console.WriteLine("Wrong input");
double r = d * Math.Pi;
Console.WriteLine(r);
compile C# program by using the commandline instead of the Visual Studio IDE:
 Open a text editor
 Save the file as helloworld.cs
 Open the command prompt tool and go to the
directory where you saved the file
 Type csc helloworld.cs and press enter to compile
 If there are no errors in your code, the command
prompt takes you to the next line and generates
helloworld.exe executable file
 Type helloworld to execute your program.
Reserved Keywords
abstract
catch
default
explicit
as
char
delegate
extern
base
checked
do
false
bool
class
double
finally
break
const
else
fixed
foreach
goto
if
implicit
in
interface
internal
is
lock
long
null
object
operator
out
private
sealed
switch
protected
short
this
public
sizeof
throw
readonly
stackalloc
true
out
(generic
modifier)
ref
static
try
ulong
unchecked unsafe
ushort
using
volatile
while
byte
continue
enum
float
in (generic
modifier)
namespac
e
case
decimal
event
for
override
params
return
string
typeof
sbyte
struct
uint
virtual
void
int
new
C# operators






Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
Miscellaneous Operators
perator
Description
Example
sizeof()
Returns the size of a data type.
sizeof(int), returns 4.
typeof()
Returns the type of a class.
typeof(StreamReader);
&
Returns the address of an variable.
&a; returns actual address of the
variable.
*
Pointer to a variable.
?:
Conditional Expression
is
Determines whether an object is of a
certain type.
as
Object obj = new
Cast without raising an exception if the cast StringReader("Hello");
fails.
StringReader r = obj as
StringReader;
*a; creates pointer named 'a' to a
variable.
If Condition is true ? Then value X :
Otherwise value Y
If( Ford is Car) // checks if Ford is
an object of the Car class.
Operator Precedence in C#
Category
Postfix
Operator
() [] -> . ++ - -
Associativity
Left to right
Unary
+ - ! ~ ++ - - (type)* & sizeof
Right to left
Multiplicative
Additive
Shift
Relational
Equality
Bitwise AND
Bitwise XOR
Bitwise OR
Logical AND
Logical OR
Conditional
*/%
+<< >>
< <= > >=
== !=
&
^
|
&&
||
?:
= += -= *= /= %=>>= <<= &= ^=
|=
,
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Right to left
Assignment
Comma
Right to left
Left to right