Introduction to C# By: Abir Ghattas Michel Barakat.

Slides:



Advertisements
Similar presentations
Language Fundamentals in brief C# - Introduction.
Advertisements

More methods and classes, 4 of 4 Math 130 Introduction to Programming Lecture # 18 Monday, October 8, 2007.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
BY BOWYA G.  This International Standard is based on a submission from Hewlett-Packard, Intel, and Microsoft, that describes a language called C#. 
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
C# Structs, operator overloading & attributes. Structs ~ Structures Structs are similar to classes: they represent data structures with data and functions.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
Lecture 9 Concepts of Programming Languages
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 20 Microsoft’s Approach 3 – C# Rob Pooley
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Programming in C# Language Overview
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Overview of C# CS331. Structure of a C# Program // Specify namespaces we use classes from here using System; using System.Threading; // Specify more specific.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Introduction to C# The New Language for. H.Mössenböck University of Linz, Austria
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Java2C# Antonio Cisternino Part II. Outline Array types Enum types Value types  differences from classes  boxing and unboxing Delegate types  Base.
FEN 2012 UCN Technology: Computer Science1 C# - Introduction Language Fundamentals in Brief.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
References types and Value types With a very brief introduction to struct and enum Reference types and Value types1.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
C# EMILEE KING. HISTORY OF C# In the late 1990’s Microsoft recognized the need to be able to develop applications that can run on multiple operating system.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Object Oriented Software Development 4. C# data types, objects and references.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Session 1 C# Basics.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
Introduction to C# Anders Hejlsberg Distinguished Engineer Developer Division Microsoft Corporation.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
INTRODUCTION BEGINNING C#. C# AND THE.NET RUNTIME AND LIBRARIES The C# compiler compiles and convert C# programs. NET Common Language Runtime (CLR) executes.
C# for C++ Programmers 1.
Abstract Data Types and Encapsulation Concepts
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Structs.
CS360 Windows Programming
C# In many ways, C# looks similar to Java
Lecture 9 Concepts of Programming Languages
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Abstract Data Types and Encapsulation Concepts
Conditional Statements
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
C# Language & .NET Platform 10th Lecture
C# Language & .NET Platform 11th Lecture
Corresponds with Chapter 5
C# Language & .NET Platform 9th Lecture
Interfaces, Enumerations, Boxing, and Unboxing
Lecture 9 Concepts of Programming Languages
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Introduction to C# By: Abir Ghattas Michel Barakat

Features of C# Very similar to Java: ▫70% Java ▫10% C++ ▫5% Visual Basic ▫15% New 2

Similarities to Java Object-orientation (single inheritance) Interfaces Exceptions Threads Namespaces (like Packages) Garbage Collection Dynamic loading of code 3

Similarities to C++ (Operator) Overloading Pointer arithmetic in unsafe code Some syntactic details 4

New Stuff (Compared to Java) Reference and output parameters Objects on the stack (Structs) Rectangular arrays Enumerations Unified type system goto Versioning 5

“Syntactic Sugar” Delegates Indexers Operator overloading foreach statements Boxing/Unboxing Attributes 6

7

Enumerations List of named constants Declaration ▫enum Color {red, blue, green} // values: 0, 1, 2 ▫enum Access {personal=1, group=2, all=4} 8

Enumerations (Example) public enum DayOfWeek { Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6 } class Program { static void Main() { DayOfWeek day = DayOfWeek.Monday; int i = (int) DayOfWeek.Monday; System.Console.WriteLine(day); // displays Monday System.Console.WriteLine(i); // displays 1 } 9

Multidimensional Arrays Jagged (like in Java) int[][] a = new int[2][]; a[0] = new int[3]; a[1] = new int[4]; int x = a[0][1]; int len = a.Length;// 2 len = a[0].Length;// 3 10

Multidimensional Arrays (Cont’d) Rectangular (more compact, more efficient access): int[,] a = new int[2, 3]; int x = a[0, 1]; int len = a.Length; // 6 len = a.GetLength(0); // 2 11

Boxing and Unboxing 12

Structs Declaration : struct Point { public int x, y;// fields public Point (int x, int y) { this.x = x; this.y = y; }// constructor public void MoveTo (int a, int b) { x = a; y = b; }// methods } Use: Point p = new Point(3, 4);// constructor initializes object on the stack p.MoveTo(10, 20);// method call 13

Structs (Cont’d) Structs are value types ▫efficient, low memory consumption, no burden for the garbage collector ▫live only as long as their container (not suitable for dynamic data structures) Fields must not be initialized at their declaration ▫struct Point { int x = 0;// compilation error} Parameter less constructors cannot be declared Can neither inherit nor be inherited, but can implement interfaces 14

Classes Declaration: class Rectangle { Point origin; public int width, height; public Rectangle() { origin = new Point(0,0); width = height = 0; } public Rectangle (Point p, int w, int h) { origin = p; width = w; height = h; } public void MoveTo (Point p) { origin = p; }} Use: Rectangle r = new Rectangle(new Point(10, 20), 5, 5); int area = r.width * r.height; r.MoveTo(new Point(3, 3)); 15

Classes VS. Structs ClassesStructs Reference Types (objects stored on the heap) Value Types (objects stored on the stack) support inheritance (all classes are derived from object) no inheritance (but compatible with object) can implement interfaces may have a destructorno destructors allowed 16

Default Constructors The default constructor initializes all fields as follows: ▫Numeric 0 ▫Enum 0 ▫Boolean false ▫char'\0‘ ▫Reference null If a constructor was declared, no default constructor is generated (for classes) 17

foreach Statement Used for iterating over collections and arrays Examples: int[] a = {3, 17, 4, 8, 2, 29}; foreach(int x in a) sum += x; strings = "Hello"; foreach(char ch in s) Console.WriteLine(ch); Queue q = new Queue(); q.Enqueue("John"); q.Enqueue("Alice");... foreach(string s in q) Console.WriteLine(s); 18

Visibility modifiers publicvisible where the declaring namespace is known - Members of interfaces and enumerations are public by default. - Types in a namespace (classes, structs, interfaces, enums, delegates) have default visibility internal(visible in the declaring assembly) privateonly visible in declaring class or struct - Members of classes and structs are private by default (fields, methods, properties,..., nested types) 19

Value Parameters(input values) void Inc(int x) { x = x + 1; } void f() { int val = 3; Inc(val); // val == 3 } -"call by value“ - formal parameter is a copy of the actual parameter - actual parameter is an expression 20

ref Parameters(transition values) void Inc(ref int x) { x = x + 1; } void f() { int val = 3; Inc(ref val); // val == 4 } -"call by reference“ -formal parameter is an alias for the actual parameter (address of actual parameter is passed) -actual parameter must be a variable 21

Variable Number of Parameters Last n parameters may be a sequence of values of a certain type. void Add (out int sum, params int[]val) { sum = 0; foreach (int i in val) sum = sum + i; } params cannot be used for ref Use: ▫Add(out sum, 3, 5, 2, 9); // sum == 19 22

23

24

Inheritance In C#, both inheritance and interface implementation are defined by the : operator, equivalent to extends and implements in Java 25