Notes on C# and software design By: Matt Boggus Some material based on slides by Roger Crawfis.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Notes on C# and object oriented programming for CSE 3902 By: Matt Boggus Some material based on slides by Roger Crawfis.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Inheritance Inheritance Reserved word protected Reserved word super
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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++
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
ASP.NET Programming with C# and SQL Server First Edition
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C++ fundamentals.
Object-Oriented Programming in C# Object-Oriented CSE Prof. Roger Crawfis.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Programming in C# Language Overview
OOP Languages: Java vs C++
Object-Oriented Programming in C# Object-Oriented CSE 668 Prof. Roger Crawfis.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Chapter 8 More Object Concepts
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
JavaScript, Fourth Edition
Programming Language C++ Xulong Peng CSC415 Programming Languages.
Introduction to C# C# is - elegant, type-safe, object oriented language enabling to build applications that run on the.NET framework - types of applications.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)
Object Oriented Software Development
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Object-Oriented Programming Chapter Chapter
1 OOP - An Introduction ISQS 6337 John R. Durrett.
Object Oriented Programming
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
Object Oriented Software Development 4. C# data types, objects and references.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
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 –
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Notes on software design and C#
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
C# for C++ Programmers 1.
Module 5: Common Type System
Object Oriented Programming in Java
CS360 Windows Programming
Object Based Programming
By: Matt Boggus Some material based on Roger Crawfis’ C# slides
Chengyu Sun California State University, Los Angeles
Notes on software design
Presentation transcript:

Notes on C# and software design By: Matt Boggus Some material based on slides by Roger Crawfis

Outline Desirable Characteristics of Software Sneak peak on programming paradigms C# syntax and examples – Classes and structs – Interfaces – Abstract classes – Class internals – Type conversion Demo of development environment

Desirable Characteristics of Software Simple Simple: composed of a single element; not compound – Complexify: to make complex – Complect: intertwine; interweave; to combine Related software design principles – Singular Responsibility Principle Singular Responsibility Principle – Separation of Concerns Separation of Concerns – Don’t Repeat Yourself Don’t Repeat Yourself Image from tring_braided-strap.jpg

Desirable Characteristics of Software Readable Function of naming and control flow See blockExample classes

Desirable Characteristics of Software Maintainable Ease of the following tasks: – isolate defects or their cause, – correct defects or their cause, – repair or replace faulty or worn-out components without having to replace still working parts, – prevent unexpected breakdowns, – maximize a product's useful life, – maximize efficiency, reliability, and safety, – meet new requirements, – make future maintenance easier, or – cope with a changed environment. We’ll come back to this with the Person*.cs examples

Desirable Characteristics of Software Reusable Reuse of existing code in other projects Identical Panel Gag image from

Two programming paradigms Object-oriented Programming – Focus on readability – Objects are an abstraction to be used by client programmers, and should follow a mental model of the actual or imagined object it represents – Objects are “nouns” that have fields “adjectives” and methods “verbs” – More discussion on why OOP is useful herehere Entity-Component System – Focus on reusability – Software should be constructed by gluing together prefabricated components like in electrical engineering – Functionality is attached to an object instead of inside its implementation

Why C#?.NET framework – Large library of features and objects; portable and integrates with software written in other languages Visual Studio – Single point of access for software development, source code control, project management, and code reviews Using Microsoft tools -> use Microsoft developed language Additional reasons – Game engines support C# -> XNA, Monogame, Unity – Used in other CSE graphics courses (Game and Animation Techniques; Game Capstone) – More discussion of pros/cons of C# herehere

C# language features Namespaces Classes – Fields – Properties – Methods – Attributes – Events Interfaces (contracts) – Methods – Properties – Events Control Statements – if, else, while, for, switch – foreach Additional Features – Operation Overloading – Structs – Enums OO Features – Type Unification – Inheritance – Polymorphism

Some syntax to know about C# No pointers. Use the dot “.” to access both namespaces and fields/methods. – Automatic memory management – garbage collection All fields are initialized by the CLR (zero for value types, null for reference types) – Structs are value type, classes are reference type Switch statements – Does not “fall-thru” (unless empty) – Can take bool’s, enum’s, integer types, and strings Expressions must be useful (no a==b;) Conditionals must evaluate to a Boolean

CLASSES VS. STRUCTS

Classes vs. structs Both are user-defined types Both can implement multiple interfaces Both can contain – Data Fields, constants, events, arrays – Functions Methods, properties, indexers, operators, constructors – Type definitions Classes, structs, enums, interfaces, delegates

ClassStruct Reference typeValue type Can inherit from any non-sealed reference type No inheritance (inherits only from System.ValueType ) Can have a destructorNo destructor Can have user-defined parameterless constructor No user-defined parameterless constructor Classes vs. structs

public class Car : Vehicle { public enum Make { GM, Honda, BMW } private Make make; private string vid; private Point location; Car(Make make, string vid, Point loc) { this.make = make; this.vid = vid; this.location = loc; } public void Drive() { Console.WriteLine(“vroom”); } } Car c = new Car(Car.Make.BMW, “JF3559QT98”, new Point(3,7)); c.Drive(); Class definition syntax

INTERFACES

Interfaces An interface defines a contract – An interface is a type – Contain definitions for methods, properties, indexers, and/or events – Any class or struct implementing an interface must support all parts of the contract Interfaces provide no implementation – When a class or struct implements an interface it must provide the implementations

Interfaces Explicit interface – Requires/ensures clauses or pre/post-conditions – Functionality is explicitly defined Implicit interface – Only method signatures are specified – Ex: IBird interface defines void Fly() Duck class implements void Fly { position.y += 5; } Penguin class implements void Fly { // no-op }

public interface IDelete { void Delete(); } public class TextBox : IDelete { public void Delete() {... } } public class ImageBox : IDelete { public void Delete() {... } } TextBox tb = new TextBox(); tb.Delete(); IDelete deletableObj = new ImageBox(); deletableObj.Delete(); deletableObj = new TextBox(); deletableObj.Delete(); Interfaces example

Object and interface design Keep it simple! – The Magical Number Seven, Plus or Minus Two The Magical Number Seven, Plus or Minus Two The average person can hold 7 ± 2 objects in memory at a time Experts recall more by “chunking” – combining multiple objects into one – Think You're Multitasking? Think Again Think You're Multitasking? Think Again The average person is bad at multi-tasking, so focus on what you’re doing if you want it done well Only provide the minimum amount of functionality required You can always add functionality later, but beware functionality bloat

ABSTRACT CLASSES

Abstract class Similar to interfaces – Cannot be instantiated – In some cases, contain no executable code – Can contain method headers/signatures and properties (more on these in a bit), but unlike interfaces they can contain concrete elements like method bodies and fields Some quirks when working with abstract classes and interfaces – A concrete class may implement an unlimited number of interfaces, but may inherit from only one abstract (or any other kind of) class – A class that is derived from an abstract class may still implement interfaces For 3902, you can use abstract classes to avoid code duplication when functionality of methods will be the same for classes that implement them

abstract class Shape { protected int x = 50; protected int y = 50; public abstract int Area(); } class Square : Shape { public int width; public int height; public override int Area() { return width * height; } public void MoveLeft() { x--; } Abstract class example See us/library/sf985hc5.aspx for a longer examplehttp://msdn.microsoft.com/en- us/library/sf985hc5.aspx Another good resource on this topic: s/6118/All-about-abstract-classes

CLASS INTERNALS

The this keyword is a predefined variable available in non-static function members – Used to access data and function members unambiguously public class Person { private string name; public Person(string name) { this.name = name; } public void Introduce(Person p) { if (p != this) Console.WriteLine(“Hi, I’m “ + name); } this name is a parameter and a field.

base The base keyword can be used to access class members that are hidden by similarly named members of the current class public class Shape { private int x, y; public override string ToString() { return "x=" + x + ",y=" + y; } internal class Circle : Shape { private int r; public override string ToString() { return base.ToString() + ",r=" + r; }

Fields A field or member variable holds data for a class or struct Can hold: – A built-in value type – A class instance (a reference) – A struct instance (actual data) – An array of class or struct instances (an array is actually a reference) – An event

Properties A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods. Properties encapsulate a getting and setting a field – Useful for changing the internal type for a field – Useful for adding code or breakpoints when getting/setting a field

Properties – examples type PropertyName { get; set; } Examples int Score { get; set; } string Name { get; } double Time { get; private set; } Code examples – Person*.cs examples [Compare maintainance] –

Modifiers Public – Accessible anywhere Protected – Accessible within its class and by derived class instances Private – Accessible only within the body of the class – (Or anywhere if you use reflection)reflection Static – Belongs to the type Instance – Belongs to the instance

Static field example public class Variable { public static int i = 5; public void test() { i=i+5; Console.WriteLine(i); } public class Exercise { static void Main() { Variable var = new Variable(); var.test(); Variable var1 = new Variable(); var1.test(); Console.ReadKey(); } Output is: 10 15

CONVERSION

Can also specify user-defined explicit and implicit conversions internal class Note { private int value; // Convert to hertz – no loss of precision public static implicit operator double(Note x) { return...; } // Convert to nearest note public static explicit operator Note(double x) { return...; } Note n = (Note) ; double d = n; Conversion operators

The is Operator The is operator is used to dynamically test if the run-time type of an object is compatible with a given type private static void DoSomething(object o) { if (o is Car) ((Car)o).Drive(); }

The as Operator The as operator tries to convert a variable to a specified type; if no such conversion is possible the result is null More efficient than using is operator – Can test and convert in one operation private static void DoSomething(object o) { Car c = o as Car; if (c != null) c.Drive(); }

Java to C# resources Java for C# developers – fairly brief syntax and terminology differences Java for C# developers The C# Programming Language for Java Developers – documentation of language differences organized by programming constructs The C# Programming Language for Java Developers Additional Suggestions from StackOverflow

VS 2012 and XNA demo