introduction to.net and c# bit-7. agenda features of.net framework .net vs. j2ee .net framework c#...

Post on 11-Jan-2016

228 Views

Category:

Documents

8 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Introduction to .Net and C#

BIT-7

Agenda Features of .Net Framework .NET vs. J2EE .Net Framework C# features and design goals C# basics Unified type system Enum Arrays

Features of .Net Framework Interoperability with other environments

Need of plateform independent applications (windows, Unix ) Microsoft Intermediate Language (MSIL)

Set of CPU independent instruction Unix compiler complies MSIL code to one that UNIX understand.

Support for developing language independent applications

Common development environment for all languages So code can be shared among application developed in diff.

languages

Support for OOPs Support for Web applications Support for Web services

.NET vs. J2EE

Both are similar in many ways: Server- and client-side model for building enterprise

applications. Virtual machine designed to inspect, load, and execute

programs in a controlled environment. APIs for creating both fat- and thin-client models. APIs for foundation services (data access, directory,

remote object calls, sockets, forms). Development environment for dynamic web pages.

J2 Enterprise Edition Language-Dependent & Platform-Independent

.NET Language-Independent & Platform Dependent (for now)

J2EE: Language-Specific, Platform- Independent

Person.java

Address.java

Company.java

Java VMPersonbytecodes

CompanybytecodesAddress

bytecodes

LinuxLinux

WindowsWindows

SolarisSolaris

Java VM

Java VM

Java VM

Deploy

.NET: Language-Independent, (Mostly) Platform- Specific

Person.vb

Address.cs

Company.cbl

CLRPersonMSIL

CompanyMSILAddress

MSIL

WindowsWindows

WindowsWindows

Others?Others?

CLR

CLR

CLR

Deploy

(Visual Basic)

(C#)

(Cobol)

.NET Framework

Framework Class LibraryFramework Class LibraryFramework Class LibraryFramework Class Library

ADO.NET

Network

XML

Security

Threading

Diagnostics

IO

Etc.

Common Language RuntimeCommon Language RuntimeCommon Language RuntimeCommon Language Runtime

Memory Management Common Type System Lifecycle Monitoring

C# VB.NET C++.NET OtherC# VB.NET C++.NET OtherC# VB.NET C++.NET OtherC# VB.NET C++.NET Other

Operating SystemOperating SystemOperating SystemOperating System

VisualVisualStudioStudio.NET.NET

VisualVisualStudioStudio.NET.NET

Common Language SpecificationCommon Language SpecificationCommon Language SpecificationCommon Language Specification

Windows FormsWindows FormsWindows FormsWindows FormsASP.NETASP.NETASP.NETASP.NETWeb Services

ASP.NET Application Services

Web Forms ControlsControls Drawing

Windows Application Services

Common Language Runtime A runtime provides services to executing

programs Standard C library, MFC, VB Runtime, JVM

CLR provided by .NET manages the execution of code and provides useful services Memory management, type system, etc. Manage threads and helps in security

Managed vs. unmanaged code

.NET Framework Class Library Framework – you can call it and it can call you Large class library

Over 2500 classes Major components

Base Class: Networking, security, I/O, files, etc. Data and XML Classes Web Services/UI Windows UI

Common Language Specification CLS is a set of rules that specifies features

that all languages should support Goal: have the .NET framework support multiple

languages CLS is an agreement among language designers

and class library designers about the features and usage conventions that can be relied upon

Some .NET Languages

• C#• COBOL• Eiffel• Fortran• Mercury• Pascal• Python

PerlSmalltalkVB.NETVC++.NETJ#.NET

….

More are planned or underdevelopment

C# Features

New OO programming language C# is advanced version of C and C++ and Specially designed for .NET environment Strong versioning support Unified type system / type safety Automatic memory management Designed to leverage the CLR

Design Goals of C# Component-orientation Everything is an object Robust and durable software Preserving your investment

C# Basics Variables

<modifiers> <datatype> <variable1, variable2………..>

Modifiers Internal Private Protected Public Read only Static

C # basics : Types of variables There are seven types of variables in c#

Static Instance Array elements {stores starting address of an array

in memory} Value parameter {without ref or out modifier} Reference parameters {with ref modifier} Out parameters {with ref modifier} Local variables

Variable Scope Block Procedure Namespace

Out parameterusing System;class Test{

static void Divide(int a, int b, out int result, out int remainder) {result = a / b;remainder = a % b;}static void Main() {

for (int i = 1; i < 10; i++)for (int j = 1; j < 10; j++) {int ans, r;Divide(i, j, out ans, out r);Console.WriteLine("{0} / {1} = {2}r{3}", i, j, ans, r);}

}}

C # Basics : Unified Type System

Value types Directly contain data Cannot be null

Reference types Contain references to objects May be null

int i = 123;string s = "Hello world";

123i

s "Hello world"

C # Basic :Unified Type System

Value (Struct) Reference (Class)

Variable holds Actual value Memory location

Allocated Stack Heap

Nullability Always has value May be null

Default value 0 null

Aliasing No Yes

= means Copy value Copy reference

Can inherit from No Yes

Unified Type System Everything is an object

All types ultimately inherit from object Any piece of data can be stored, transported, and

manipulated with no extra work

Mem oryStream FileStream

Stream Hashtable int double

object

Unified Type System Polymorphism

The ability to use or store an object without knowing its precise type

void Poly(object o) { Console.WriteLine(o.ToString()); }

Poly(42);Poly(“abcd”);Poly(12.345678901234m);Poly(new Point(23,45));

Unified Type System Boxing copies a value type into a reference

type Unboxing copies it out

int i = 123;

object o = i;

int j = (int)o;

123i

o

123j123

System.Int32

Unified Type System Unboxing

Inverse operation of boxing Copies the value out of the box

Copies from reference type to value type Requires an explicit conversion

May not succeed

using System;struct Struct1{Public int Value;}class Class1{public int Value = 0;}class Test{static void Main() {Struct1 val1 = new Struct1();Struct1 val2 = val1;val2.Value = 123;Class1 ref1 = new Class1();Class1 ref2 = ref1;ref2.Value = 123;Console.WriteLine("Values: {0}, {1}", val1.Value, val2.Value);Console.WriteLine("Refs: {0}, {1}", ref1.Value, ref2.Value);}}

Values: 0, 123Refs: 123, 123

Boxing and UnboxingExample

Unified Type System Benefits

Enables polymorphism across all types Collection classes work with all types Eliminates need for wrapper classes

Lots of examples in .NET Framework

Hashtable t = new Hashtable();t.Add(0, "zero");t.Add(1, "one");t.Add(2, "two"); string s = string.Format(

"Your total was {0} on {1}", total, date);

Predefined TypesIntegral Types

C# Type System Type Size (bytes) Signed?

sbyte System.Sbyte 1 Yes

short System.Int16 2 Yes

int System.Int32 4 Yes

long System.Int64 8 Yes

byte System.Byte 1 No

ushort System.UInt16 2 No

uint System.UInt32 4 No

ulong System.UInt64 8 No

C# Type System Type Size (bytes)float System.Single 4

double System.Double 8

Statements Ecma : C# language specifications

Page 29

Operators Is

Int i=10;If(i is object)

{ …………….}

Sizeof sizeof(int);

Typeoftypeof(string);

Types User-defined Types User-defined types

Enumerations enum

Arrays int[], string[]

Interface interface

Reference type class

Value type struct

Function pointer delegate

User defined Types :Enumsenum Color{Red,Blue,Green}class Shape{public void Fill(Color color) {

switch(color) {case Color.Red:.break;case Color.Blue:.break;

User defined Types : Arrays Arrays allow a group of elements of a specific

type to be stored in a contiguous block of memory

Arrays are reference types Derived from System.Array Zero-based Can be multidimensional

Arrays know their length(s) and rank Provide bounds checking

User defined Types : Arrays

Declare

Allocate

Initialize

Access and assign

Enumerate

int[ ] primes;

int[ ] primes = new int[9];

int[ ] prime = new int[ ] {1,2,3,5,7,11,13,17,19}; int[ ] prime = {1,2,3,5,7,11,13,17,19};

prime2[i] = prime[i];

foreach (int i in prime) Console.WriteLine(i);

Single-dimensional arraysusing System;class Test{

static void Main() {int[] arr = new int[5];for (int i = 0; i < arr.Length; i++)

arr[i] = i * i;for (int i = 0; i < arr.Length; i++)

Console.WriteLine("arr[{0}] = {1}", i, arr[i]);}

}

User defined Types : Arrays Multidimensional arrays

Rectangular int[,] matR = new int[2,3]; Can initialize declaratively int[,] matR = new int[2,3] { {1,2,3}, {4,5,6} };

Jagged An array of arrays int[][] matJ = new int[2][]; Must initialize procedurally

User defined Types : Arraysclass Test{

static void Main() {int[] a1; // single-dimensional array of intint[,] a2; // 2-dimensional array of intint[,,] a3; // 3-dimensional array of intint[][] j2; // "jagged" array: array of (array

of int)int[][][] j3; // array of (array of (array of int))

}}

User defined Types : Arraysclass Test{

static void Main() {int[] a1 = new int[] {1, 2, 3};int[,] a2 = new int[,] {{1, 2, 3}, {4, 5, 6}};int[,,] a3 = new int[10, 20, 30];int[][] j2 = new int[3][];

j2[0] = new int[] {1, 2, 3};j2[1] = new int[] {1, 2, 3, 4, 5, 6};j2[2] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};

}}

Parameter array A parameter array enables a many-to-one relationship:

many arguments can be represented by a single parameter array. In other words, parameter arrays enable variable length argument lists.

class Test{static void F(params int[] args) {Console.WriteLine("# of arguments: {0}", args.Length);for (int i = 0; i < args.Length; i++)Console.WriteLine("\targs[{0}] = {1}", i, args[i]);}static void Main() {F();F(1);F(1, 2);F(1, 2, 3);}}

THANK YOU!

top related