introduction (1)

Download Report

Transcript introduction (1)

Distributed Systems (236351)
Tutorial 1 - Getting Started with Visual Studio
C# .NET
Staff
 Lecturer: Assoc. Prof. Roy Friedman.
 Teaching Assistant: Noam Mori.




2
Email: [email protected]
Office: 325.
Office hours: Monday 13:30-14:30.
Phone: 04-829-4307.
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
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




4
Grouping: {}
Statements terminated by ";"
C operators: ++ % != += && & ^, >>, ?: …
C like control:
 if () … else …
 for (…; …; …) … break …
 while (…) … continue …
 do … while (…)
 switch (…) … case … default
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

5
In the Visual Studio .NET environment, select File | New | Project
from the menu.
Hello World application cont.
 Select Visual C# Projects on the left and then Console
Application on the right.
6
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
Class1.cs
using System; //Namespace
namespace project_name
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
}
8
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
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?



10
Developed by Microsoft
Compiles to the CLR "Common Language Interface"
Support for "unsafe" features, including pointers.
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
Value/Reference Semantics
 Value Types

Simple types: char, int, float, …

Enum types
public enum Color {Red, Blue, Green}

Struct types
public struct Point { public int x, y; }
 Reference Types

12
Classes, Interfaces, Delegates
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:


13
No inheritance
May implement interfaces
Accessibility
 Five Levels





public
protected
private
internal
protected
Unlimited access
This class and all subclasses
This class only
internal
 Default Levels






14
namespace
enum
class
interface
struct
others
public
public
private
public
private
internal
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







15
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
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();
}
Window w = new Window("Initial Title");
}
Console.WriteLine(w.Title);// increment n_read
…
{
w.Title = "My Title"; // redraw
16