Вы находитесь на странице: 1из 32

C# Programming and .

NET Concepts

By, Chaman Shareef S. H.

Recap previous programming languages.


C is a building block for many other currently known languages Drawbacks of C: There is no strict type checking (for ex: we can pass an integer value for the floating data type) There is no runtime checking in C language C does not have OOPS feature C doesn't have the concept of constructors and destructors C doesn't have the concept of namespace

Benefits of C++ over C


Stronger Type Checking. Object oriented programming concept is introduced. Got a concept of constructor and destructor. Logical organization of code into logically related 'paragraphs' complete with their necessary declarations.

Drawbacks of C++
Not platform independent. Code is not protected. It is not multitask. No built in support for threads. Memory management is complicated. Does not provide efficient means for garbage collection Gets complex when u want to develop a graphics rich application in C++.

Benefits of java
Java uses automatic memory allocation and garbage collection Object-oriented approach Java is platform-independent Java is secure Java is multithreaded

Drawbacks of java
Java can be considerably slower Exceptions not caught within a method must be declared as thrown by that method. A worse problem is that the programs do not always work correctly even if they are written correctly, because a Java virtual machine may be written incorrectly.

Why should I move to C#?


Usually it is much more efficient than java and runs faster C# program is compiled to an intermediate language CIL CIL is a standard language, while java bytecodes aren't. Automatic garbage collection Simplified multithreading Operator overloading. It can make development a bit trickier but they are optional and sometimes very useful It has more primitive types (value types), including unsigned numeric types

Conti
Formalized concept of get-set methods, so the code becomes more legible More clean events management (using delegates) Pointers no longer needed

.NET Framework
VB C++ C# JScript J# Common Language Specification Visual Studio.NET Visual Studio.NET ASP.NET Web Forms Web Services ADO.NET and XML Base Class Library Common Language Runtime Operating System
9

Windows Forms

Building Blocks of .NET


CLR (Common Language Runtime)

To locate, load, and manage .NET types Automatic memory management, language integration, and type safety Describes all possible data types and programming constructs supported by the runtime A set of rules that defines a subset of types and specifications
10

CTS (Common Type System)

CLS (Common Language Specification)

CLR (Common Language Runtime)


CLR sits on top of OS (same as JVM of Java) CLR loads modules containing executables and executes them Code may be managed or unmanaged Managed code consists of instructions in pseudo random code called CIL (Common Intermediate Language). CIL instructions are JIT compiled into native machine code at runtime JIT compiled methods reside in cache until the applications life time Advantages of managed code: type safety, memory management, and code verification security CLR can translate code from C#, J#, C, C++, VB, and Jscript into CIL. CLR doesnt launch a new process for every application. It launches one process and hosts individual applications in application domains 11

Base Class Libraries


Encapsulates various primitives like: threads, file IO, graphical rendering, and other interaction with HW devices It also provides: database manipulation, XML integration, Web-enabled front-end.
Data Access Threading GUI File IO
Base Class Libraries

XML/SOAP

Common Language Runtime


CTS CLS
12

Understanding Assemblies
Assemblies contains CIL code, which is conceptually similar to java bytecode in that it is not compiled to platform specific instruction. C# .NET compiler doesn't generate machine code. It is compiled into "assembly"
13

Assembly
C# source code

Metadata

C# .NET Compiler

IL Assembly

Intermediate Language (IL/CIL): Same as first pass of compiler. It can't be executed (it is not in binary format) Metadata Describes the assembly contents No need for component registry Each assembly includes information about references to other assemblies E.g. If you have a class called Car in a given assembly, the type metadata describes Car's base class, which interfaces are implemented by Car, description of members of Car.
14

Assembly
When CLR loads your application, it examines your program's metadata to know which external assemblies are required for execution Private assemblies Used by single application Is not shared Most preferred method Shared assemblies Intended for multiple applications Global Assembly Cache Manifest The metadata of assemblies: version, list of externally defined assemblies, etc.
15

Example of CIL
CIL sits above a specific compiler (C#, J#, etc.) The associated compiler emits CIL instructions } public class Calc { public int Add(int x, int y) { return x + y; } }

using System; namespace Calculator { public class CalcApp { public static void Main(string[] args) { Calc c = new Calc(); int ans = c.Add(10, 84); Console.WriteLine(ans); Console.ReadLine(); } }

All .NET aware languages emit same CIL instructions

16

CIL of Add() Method


.method public hidebysig instance int32 Add(int32 x, int32 y) cil managed { // Code size 8 (0x8) .maxstack 2 .locals init ([0] int32 CS$00000003$00000000) IL_0000: ldarg.1 IL_0001: ldarg.2 IL_0002: add IL_0003: stloc.0 IL_0004: br.s IL_0006 IL_0006: ldloc.0 IL_0007: ret } // end of method Calc::Add

17

Manifest

External Assembly

.assembly extern mscorlib { .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) .ver 1:0:5000:0 } .assembly ConsoleApplication1 { .hash algorithm 0x00008004 .ver 1:0:2058:39833 } .module ConsoleApplication1.exe // MVID: {51BE4F31-CBD0-4AE6-BC9D-F9A4976795FD} .imagebase 0x00400000 .subsystem 0x00000003 .file alignment 4096 .corflags 0x00000001 // Image base: 0x070b0000

// .z\V.4..

18

CIL to Execution
Desktop JIT Server

CIL

Pocket PC

Jitter compiles CIL instructions on the fly into corresponding machine code and cache it. This is useful for not recompiling, if the same method is called again
19

Common Type System (CTS)


CTS is a formal specification that describes how a given type must be defined for CLR CTS Class Type CTS Structure Type CTS Interface Type CTS Enumeration type CTS Delegate type
20

CTS Class Type


Same as C++ class Can contain members: methods, properties, events, etc. Support for abstract members that define a polymorphic interface for derived classes Multiple inheritance is not allowed

21

CTS Class Characteristics


"sealed"? sealed classes can't function as base classes Implement any interfaces? An interface is a collection of abstract members Abstract or Concrete? Abstract classes (to define common behaviors for derived) can't be created directly but concrete classes can. Visibility? visibility attribute to know whether external assemblies can use it.
22

CTS Structure types


Same as C/C++ Derived from a common base class System.ValueType Same as pure abstract class of C++ A description of work that a derived class can perform Similar to a class, but can never be instantiated To group name/value pairs under a specific name Default Storage: System.Int32 (could be changed) Same as C's function pointer (System.MulticastDelegate) Useful for event handling (ASP .NET)
23

CTS Interface Type


CTS Enumeration type CTS Delegate type

Intrinsic CTS Data Types


.NET Base Type
System.Byte System.SByte System.Int16 System.Int32 System.Int64 System.UInt64 System.Single System.Double System.Object System.String System.Boolean

C# Type
Byte sbyte short int long ulong float double object string bool
24

Common Language Specification

(CLS)
Set of guidelines that describe the minimal and complete set of features a given .NET aware compiler must support C# uses + for concatenation whereas VB .NET uses & C# allows operator overloading but VB .NET does not! The void functions may differ in syntax:
' VB .NET // C#

Public Sub Foo() '. End Sub

public void Foo() { . }


25

CLR

.NET Source Code

.NET Compiler DLL or EXE (CIL) mscoree.dll Class Loader

Base Class Libraries (mscorlib.dll)

Jitter Platform Specific code

mscoree.dll MicroSoft Common Object Runtime Execution Engine

Execute .NET Execution Engine


26

.NET Namespace
MFC, Java, VB 6.0 have predefined set of classes; C# doesn't C# uses namespace concept Any language targeting the .NET runtime makes use of the same namespaces and same types as C# System is the root namespace
27

Example in C#

System Namespace

using System; public Class MyApp { Console class in public static void Main() System Namespace { Console.WriteLine("Hello World"); } }
28

Example in VB .NET
Imports System Public Module MyApp Sub Main() Console.WriteLine("Hello World") End Sub End Module

29

Example in Managed C++


#using <mscorlib.dll> using namespace System; void Main() { Console::WriteLine("Hello World"); }

30

Sample .NET namespaces


System System.Collections System.Data System.Data.Common System.Data.OleDb System.Data.SqlClient System.IO System.Drawing System.Drawing.2D System.Threading primitive types, garbage collection, etc Container objects: ArrayList, Queue, etc. For Database manipulations ADO .NET

file IO, buffering, etc. GDI+ primitives, bitmaps, fonts, icons, etc. Threads
31

Conclusion
Came up with limitations of previous programming languages Overview of how .NET and C# came up with those limitations Frame work of .NET Explored about CLR and JIT

Вам также может понравиться