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: J2 Enterprise Edition 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 Linux Java VM Java VM Person bytecodes Deploy Windows Company bytecodes Java VM Address bytecodes Solaris Address.java Company.java Java VM
.NET: Language-Independent, (Mostly) Platform- Specific Person.vb Windows (Visual Basic) CLR CLR Person MSIL Deploy Windows Company MSIL CLR Address MSIL Others? Address.cs (C#) Company.cbl CLR (Cobol)
.NET Framework C# VB.NET C++.NET Other Visual Studio .NET Common Language Specification Framework Class Library ASP.NET Windows Forms Web Services Web Forms Controls Drawing ASP.NET Application Services Windows Application Services ADO.NET XML Threading IO Network Security Diagnostics Etc. Common Language Runtime Memory Management Common Type System Lifecycle Monitoring Operating System
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# Perl COBOL Smalltalk Eiffel VB.NET Fortran Mercury Pascal Python Perl Smalltalk VB.NET VC++.NET J#.NET …. More are planned or under development
C# Features Design Goals of C# 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 First of all, C# was designed from the ground up to support components concepts like events, methods and properties. Second, everything is an object, which allows us to create some really clean designs. Third, it was designed to make it easy to create robust and maintainable software And finally, it should be able to integrate easily with everything that already exists, preserving your investment.
C# Basics Variables Modifiers <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 parameter using 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 123 i s "Hello world" int i = 123; string 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 null Aliasing No Yes = means Copy value Copy reference Can inherit from Actually, structs are aliased when passed through “ref” parameters, as discussed later. Even still, only one alias can be in scope at a time.
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 Everything inherits from object, including primitive types, structs or classes.
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()); } The example shows a function, Poly(), that is called with different types of arguments. 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 i 123 int i = 123; object o = i; int j = (int)o; o System.Int32 Line 2 illustrates boxing. Line 3 illustrates unboxing. 123 j 123
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
Boxing and Unboxing Example 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, 123 Refs: 123, 123 Boxing and Unboxing Example
Unified Type System Benefits Lots of examples in .NET Framework Enables polymorphism across all types Collection classes work with all types Eliminates need for wrapper classes Lots of examples in .NET Framework Main benefits of the Unified Type System: No need of wrapper code to use base types in collections or arrays No Variants anymore 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 Types Integral Types C# Type System Type Size (bytes) Signed? sbyte System.Sbyte 1 Yes short System.Int16 2 int System.Int32 4 long System.Int64 8 byte System.Byte No ushort System.UInt16 uint System.UInt32 ulong System.UInt64 C# Type System Type Size (bytes) float System.Single 4 double System.Double 8
Statements Ecma : C# language specifications Page 29
Operators Is Sizeof Typeof Int i=10; If(i is object) { …………….} { …………….} Sizeof sizeof(int); Typeof typeof(string);
Types User-defined Types Enumerations enum Arrays int[], string[] Interface interface Reference type class Value type struct Function pointer delegate
User defined Types :Enums enum Color { Red, Blue, Green } class Shape public void Fill(Color color) { switch(color) { case Color.Red: . break; case Color.Blue:
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 arrays using System; class Test { static void Main() { int[] arr = new int[5]; for (int i = 0; i < arr.Length; i++) arr[i] = i * 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 : Arrays class Test { static void Main() { int[] a1; // single-dimensional array of int int[,] a2; // 2-dimensional array of int int[,,] a3; // 3-dimensional array of int int[][] j2; // "jagged" array: array of (array of int) int[][][] j3; // array of (array of (array of int)) }
User defined Types : Arrays class 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!