400 likes | 628 Views
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
E N D
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)
Java VM Java VM Java VM J2EE: Language-Specific, Platform- Independent Person.java Linux Java VM Person bytecodes Deploy Windows Company bytecodes Address bytecodes Solaris Address.java Company.java
CLR CLR CLR .NET: Language-Independent, (Mostly) Platform- Specific Person.vb Windows (Visual Basic) CLR Person MSIL Deploy Windows Company MSIL Address MSIL Others? Address.cs (C#) Company.cbl (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 Common Type System Lifecycle Monitoring Memory Management 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# • COBOL • Eiffel • Fortran • Mercury • Pascal • Python Perl Smalltalk VB.NET VC++.NET J#.NET …. More are planned or under development
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 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++) { intans, r; Divide(i, j, out ans, out r); Console.WriteLine("{0} / {1} = {2}r{3}", i, j, ans, r); } } }
i 123 s "Hello world" 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";
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
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 123 i int i = 123; object o = i; int j = (int)o; System.Int32 o 123 123 j
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, 123 Refs: 123, 123 Boxing and Unboxing Example
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);
Statements • Ecma : C# language specifications • Page 29
Operators • Is Int i=10; If(i is object) { …………….} • Sizeof sizeof(int); • Typeof typeof(string);
Types User-defined Types • User-defined types
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: . 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 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; 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 : 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(paramsint[] args) { Console.WriteLine("# of arguments: {0}", args.Length); for (inti = 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); }}