DAD 2017-18 Lab. 1 Introduc7on to C#
Summary 1..NET Framework Architecture 2. C# Language Syntax C# vs. Java vs C++ 3. IDE: MS Visual Studio Tools Console and WinForm Applica7ons
1..NET Framework Introduc7on Architecture
.NET Framework Architecture
Common Language Run7me Execu7on Environment Memory Management Garbage collec7on Common type system Primi7ve types (int,double) Stack allocated Assignment copy values Freed at the block s end User- defined: struct, enum Classes, arrays,... Heap allocated Assignment copies reference Garbage collected
Framework Class Library System System.Collec7ons System.Drawing System.IO System.Data System.Windows.Forms System.Web.UI System.Web.Services...
.NET: Main Advantages Virtual execu7on environment. Many libraries. APIs for web development. Language interoperability. New standard: C#
C# 2.0 Basic Syntax: It s very similar to Java...
Hello World using System; public class HelloWorld { public static void Main(string[] args) { Console.WriteLine( Hello World! );
A simple Class public class Person { private string name; private int age; public Person(string name, int age) { this.name = name; this.age = age; public void ShowInfo() { Console.WriteLine( {0 is {1 years old., name, age); [...] Person client = new Person( John, 25); client.showinfo();
C#: Type System String string Name; Array int[] table; Object Class class Person {... Enum enum State { On, Off ValueType Struct struct Point { int x; int y; bool byte sbyte decimal char short ushort float int uint decimal account; double long ulong
C#: Everything Is an Object (2) int val = 10; object obj = val; int k = (int) obj; // k becomes 10 val 10 obj 10 System.Int32 Boxing k 10 Unboxing
Execu7on Control if, for, do, while, switch, foreach... switch without fall- through: switch a { case 2: x = 4; goto case 3 // explicit fall- through case 3:... Foreach: int[] table = {1, 2, 3; foreach (int i in table) Console.WriteLine( {0, i);
Classes Name hierarchy: namespaces Simple class inheritance. Mul7ple interface inheritance. Class members: Fields, methods, proper7es, indexers, events,.. Access levels: public, protected, internal, private Members can be static or instance. abstract members also possible.
C#: Inheritance public class Person { private string name; public class Employee : Person { private string company; public Person(string name) { this.name = name; public virtual void ShowInfo() { Console.WriteLine( Name:{0, name); public Employee(string name, int company) : base(name) { this.company = company; public override void ShowInfo() { base.showinfo(); Console.WriteLine( Company: {0, company); By default, methods are not virtual!
C#: Inheritance
C#: Parameter Passing ref passing value- types as references {... char c= c ; g(ref c); out passing non- ini7alized value- types as references: {... int x; f(out x); void g(ref char pc) { pc = x ; void f(out int x) { x=2;
C#: Parameter Passing params passing a variable number of parameters public static void Main() { UseParams(1, 'a', "test"); int[] myarray = new int[3] {10,11,12; UseParams(myarray); public static void UseParams(params object[] list) { for ( int i = 0 ; i < list.length ; i++ ) Console.WriteLine(list[i]); Console.WriteLine();
C#: Operator Redefini7on It is possible to redefine exis7ng operators. MyList A = new MyList(); MyList B = new MyList(); A.Add(1); A.Add(2); B.Add(3); B.Add(4); MyList C = A + B; // Joins both lists
C#: Operator Redefini7on (2) public class MyList { private object[] Elements;... MyList A = new MyList(); MyList B = new MyList ();... MyList C = A + B; public static MyList operator+(lista a, Lista b) { MyList result = new MyList (); // Copy the elements from <a> and <b> // into the return value return result;
C#: unsafe code Support for advanced programming, such as direct pointer manipula7on. Direct pointer and type manipula7ons must be performed within an unsafe block. int total = 0; unsafe { int* ptr = &total; *ptr = 10; public unsafe void FastCopy(byte* dst, byte* src, int count) { for (int i=0; i<count; i++) *dst++ = *src++;
C#: XML Documenta7on /// <summary> /// This method calculates a person s wages, /// based on the number of working days. /// </summary> /// /// <param name= workingdays"> /// The total of full working days. /// </param> /// /// <returns>the person s salary</returns> public int CalculateWages(int workingdays) {...
C# vs. C++ GC destroys unreachable objects. Reference types and value types. Boxing, unboxing. Method redefini7on must be explicit. Booleans are integers. switch without explicit fall- through. Unassigned variables cannot be used. (out) There are no global methods.
C# vs. Java Support for less than one and more than one class per file. Only one Main per assembly. Filename not related to contained classes. Namespaces instead of packages. goto Operator redefini7on. Unsafe code. Passing value- types by reference using ref. Output is an executable (.exe) or a library (.dll).
3. IDE: MS Visual Studio Tools Console / Win Form Applica7ons
IDE Development Environment.Net Framework 2.0 (minimum) Visual Studio.NET Tools Editor Compiler Debugger Projects Console applica7on Windows applica7on Class library ASP.Net web service ASP.Net web applica7on...