distributed systems (236351) tutorial 1 - getting started with visual studio c#.net

16
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C# .NET

Upload: scarlett-nash

Post on 13-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET

Distributed Systems (236351)

Tutorial 1 - Getting Started with Visual Studio C# .NET

Page 2: Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET

Staff

Lecturer: Assoc. Prof. Roy Friedman. Teaching Assistant: Noam Mori.

Email: [email protected] Office: 325. Office hours: Wednesday 13:30-14:30. Phone: 04-829-4307.

2

Page 3: Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET

Course Info

Home page: http://webcourse.cs.technion.ac.il/236351 Three mandatory programming assignments. Requirements:

Working knowledge of Java / C# Basic knowledge of OOP concepts Basic knowledge of network concepts (sockets, protocols)

No textbook, look at the home page for manuals, tutorials and additional resources

3

Page 4: Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET

C#

Designer: Anders Hejlsberg (Microsoft) Designer of Turbo Pascal, Visual J++, Delphi (Borland)

C Dynasty: Play on Words C++ increment C by one. C# the musical note half tone above C

Yet another curly bracket programming language Grouping: {} Statements terminated by ";" C operators: ++ % != += && & ^, >>, ?: … C like control:

if () … else … for (…; …; …) … break … while (…) … continue … do … while (…) switch (…) … case … default

4

Page 5: Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET

Hello World application

Development in Visual Studio is organized around solutions, which contain one or more projects. For this tutorial, we will create a solution with a single C# project.

Creating a New Project In the Visual Studio .NET environment, select File | New | Project

from the menu.

5

Page 6: Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET

Hello World application cont.

Select Visual C# Projects on the left and then Console Application on the right.

6

Page 7: Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET

Hello World application cont.

Specify the name of your project, location in which to create the project and the name of your solution. The project directory will be created automatically by Visual Studio

Click OK and you're on your way!

7

Page 8: Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET

Class1.cs

using System; //Namespacenamespace project_name{ class Program { static void Main(string[] args) { Console.WriteLine("Hello World"); } }}

8

Page 9: Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET

Namespaces

Namespaces are used to define scope in C# applications Multiple source code files can contribute to the same namespace The using directive permits you to reference classes in the namespace without

using a fully qualified name

class Class1 {

static void Main(string[] args){ System.Console.WriteLine("Hello World");

} }

9

Page 10: Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET

C# Design Principles

Closer to Java than to C++. Programmer Protection:

Static Typing Strong Typing Array Bounds Checking Garbage Collection Check against using uninitialized variables

Better Java? Developed by Microsoft Compiles to the CLR "Common Language Interface" Support for "unsafe" features, including pointers.

10

Page 11: Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET

Object Oriented Purity

Global Variables? No. All variables are defined in functions/classes.

Global Routines? No. All routines (functions) are defined in classes.

Non OO Types? No. Even primitive types belong in the OO hierarchy.

Preprocessor? Yes.

11

Page 12: Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET

Value/Reference Semantics

Value Types Simple types: char, int, float, … Enum types

Struct types

Reference Types Classes, Interfaces, Delegates

public enum Color {Red, Blue, Green}public enum Color {Red, Blue, Green}

public struct Point { public int x, y; }public struct Point { public int x, y; }

12

Page 13: Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET

Inheritance Hierarchy

Classes: Single Inheritance Common root: System.Object Unextendable classes: denoted by keyword sealed Static classes: denoted by keyword static

No non-static members No instances

Interfaces: Multiple Inheritance hierarchy May be implemented by classes and structs

Structs: No inheritance May implement interfaces

13

Page 14: Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET

Accessibility

Five Levels public Unlimited access protected This class and all subclasses private This class only internal protected internal

Default Levels namespace public enum public class private interface public struct private others internal

14

Page 15: Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET

Class/Struct Member Kinds

Instance Constructors: similar to C++/Java constructors Finalizer: Syntax as C++ destructor; semantics as Java finalizer. Static Constructors: similar to Java static initializers Constants: value computed at compile time

implicitly static Instance Readonly Fields: with readonly keyword

initialized by constructor / static constructor Instance Fields: like Java/C++ Static Fields: with static keyword Static Readonly Fields: Initialized by static constructor only Methods & Static Methods: like Java/C++ Indexers: array access implemented by methods Properties (and static properties): field access implemented by

methods

15

Page 16: Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET

Properties

Property: a field implemented with methods Varieties: read only, write only, read-write Contextual keywords: get, set, value

public struct Window { public int n_read = 0; private string title; public string Title { // read-write property get { // property getter method n_read++;

return title; } set { // property setter method if (title == value)// implicit parameter return; title = value; redraw(); } }…{

public struct Window { public int n_read = 0; private string title; public string Title { // read-write property get { // property getter method n_read++;

return title; } set { // property setter method if (title == value)// implicit parameter return; title = value; redraw(); } }…{

Window w = new Window("Initial Title");

Console.WriteLine(w.Title);// increment n_read

w.Title = "My Title"; // redraw

Window w = new Window("Initial Title");

Console.WriteLine(w.Title);// increment n_read

w.Title = "My Title"; // redraw16