padi-13-14-labs-aula1-slides en -...
TRANSCRIPT
Summary
• 1. .NET Framework – Architecture
• 2. C# Language – Syntax – C# vs. Java vs C++
• 3. IDE: MS Visual Studio – Tools – Console and WinForm Applica7ons
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#
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
Object
String
Array
ValueType
Enum
Struct
int
short
byte
char
float
decimal
long
uint
ulong
sbyte
ushort
bool
double
Class
string Name;
int[] table;
class Person { ... }
decimal account;
enum State { On, Off}
struct Point { int x; int y; }
C#: Everything Is an Object (2) int val = 10; object obj = val; int k = (int) obj; // k becomes 10
System.Int32 } Boxing 10
10
val
obj
} Unboxing 10 k
Execu7on Control
• if, for, do, while, switch, foreach... • switch without fall-‐through:
• Foreach:
switch a { case 2:
x = 4; goto case 3
// explicit fall-‐through case 3:
...
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 Person(string name) { this.name = name; } public virtual void ShowInfo() { Console.WriteLine(“Name:{0}”, name); } }
• By default, methods are not virtual!
public class Employee : Person { private string company; public Employee(string name, int company) : base(name) { this.company = company; } public override void ShowInfo() { base.ShowInfo(); Console.WriteLine(“Company: {0}”, company); } }
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 f(out int x) {
x=2; }
void g(ref char pc) {
pc =‘x’; }
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; ... 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; } }
MyList A = new MyList(); MyList B = new MyList (); ... MyList C = A + B;
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).