160 likes | 180 Views
Learn the basics of C# programming and Visual Studio for distributed systems. Understand C# design principles and object-oriented concepts. Explore namespaces, design principles, inheritance hierarchy, and member kinds.
E N D
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C# .NET
Staff Lecturer: Assoc. Prof. Roy Friedman. Teaching Assistant: Noam Mori. Email: noam@cs.technion.ac.il Office: 325. Office hours: Wednesday 13:30-14:30. Phone: 04-829-4307. 2
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
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
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.
Hello World application cont. • Select Visual C# Projects on the left and then Console Application on the right.
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!
Class1.cs using System; //Namespace namespace project_name { class Program { static void Main(string[] args) { Console.WriteLine("Hello World"); } } }
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"); } }
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.
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.
Value/Reference Semantics • Value Types • Simple types: char, int, float, … • Enum types • Struct types • Reference Types • Classes, Interfaces, Delegates publicenum Color {Red, Blue, Green} public struct Point { public int x, y; }
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
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
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 readonlykeyword • 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 publicstruct Window {publicintn_read = 0;private string title; public string Title { // read-write property get { // property getter methodn_read++; return title; }set { // property setter method if (title == value)// implicit parameterreturn; title = value; redraw(); } } … { Window w = new Window("Initial Title"); Console.WriteLine(w.Title);// incrementn_read w.Title = "My Title"; // redraw