100% found this document useful (1 vote)
64 views

CSharp and Software Design

This document provides an overview and examples of software design principles and the C# programming language. It discusses characteristics of high-quality software such as being simple, readable, maintainable and reusable. It also covers C# syntax including classes, structs, interfaces, abstract classes and class internals. Examples are provided for object-oriented and component-based programming paradigms.

Uploaded by

Ramesh Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
64 views

CSharp and Software Design

This document provides an overview and examples of software design principles and the C# programming language. It discusses characteristics of high-quality software such as being simple, readable, maintainable and reusable. It also covers C# syntax including classes, structs, interfaces, abstract classes and class internals. Examples are provided for object-oriented and component-based programming paradigms.

Uploaded by

Ramesh Babu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Notes on software

design and C#
Outline

• Defining high quality software

• C# syntax and examples


• Classes and structs
• Interfaces
• Abstract classes
• Class internals
• Type conversion

• Demo of development environment


Some characteristics of
high quality software

• Simple
• Readable
• Maintainable
• Reusable

• For more specific qualities/principles,


read up on SOLID (object-oriented design)
Simple

• Composed of a single
element; not compound
• Complexify: to make
complex
• Complect: intertwine;
interweave; to combine
Image from
https://bagntell.files.wordpress.com/
2012/02/four_string_braided-strap.jpg
• Related software design
principles
• SOLID: S
ingular Responsibility Princip
le
• SOLID: I
nterface segregation principl
e
• Separation of Concerns
Cohesion

High cohesion Low cohesion


int Square(int num) void DoStuff()

{ {
BuyTickets();
return num * num;
MakeCoffee();
} DriveToMuseum();
CashLotteryTicket();
}
Readable

• Function of
• Naming conventions
• Formatting (white space)
• Control flow (problem decomposition)
• Reader’s experience with the language

• See blockExample classes


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


Coupling

Loose Coupling Tight Coupling


IController controller; if(myGame.KeyboardCotnroller.
controller = new KeyA.isPushed)
KeyboardController(); {
controller.Update(); Jump();
}
Reusable

• Reuse of existing
code in other
projects

• Related software
design principle
• Composition over inh
eritance

Identical Panel Gag image from


http://tvtropes.org/pmwiki/pmwiki.php/ComicBook/Invincible
Two programming paradigms

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

• Fits with
• .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

• 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# here
• More discussion of pros/cons of C# specific to game development
here; this subset of comments has some good insights
C# language features

• Namespaces
• Classes
• Fields
• Properties
• Methods
• Events
• Structs
• Enums
• Interfaces (contracts)
• Methods
• Properties
• Events
• Control Statements
• if, else, while, for, switch, foreach
C# and Java similarities

• All classes are objects


• Compare System.Object to java.lang.Object

• Similar compilation and runtime to Java


• Compare Java Virtual Machine to Common Language
Runtime

• Heap-based allocation
• Use “new” keyword to instantiate
• Automatic garbage collection
Java to C# resources

• Java for C# developers – fairly brief syntax and


terminology differences

• The C# Programming Language for Java Develo


pers
– documentation of language differences
organized by programming constructs

• Additional Suggestions from StackOverflow


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
Classes vs. structs

Class Struct
• Reference type • Value type

• Original instance of • A copy of the object


the object can be is made and
modified during operated on inside
execution of method method bodies
bodies

Note: Many types in XNA are defined as structs (ex: Vector2 and Rectangle)
Can pass structs as reference type using ‘ref’ keyword
Class syntax example
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; Car c =
this.vid = vid; new Car(Car.Make.BMW,
“JF3559QT98”,
this.location = loc; new Point(3,7));
} c.Drive();
public void Drive()
{ Console.WriteLine(“vroom”); }
}
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
• SOLID: Depend upon abstractions not concretions
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 }
Interfaces example
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();
Object and interface design

• Keep it simple!
• 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
• 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
• SOLID: Open to extension, but closed to modification
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 class that is derived from an abstract class may still implement
interfaces
• A concrete class may implement an unlimited number of
interfaces, but may inherit from only one abstract (or concrete)
class
public abstract class Shape
{
protected int x = 50;
protected int y = 50;
Abstract class
example
public abstract int
Area();
}
Methods marked as abstract have
public class Square : Shape
no bodies and must be overridden
{
public int width;
public int height; Methods marked as virtual have
bodies and may be overridden
public override int Area()
{ See
return width * height; http://msdn.microsoft.com/en-us/
} library/sf985hc5.aspx
for a longer example
public void MoveLeft()
{
x--;
}
}
Class internals
this

• 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);
}
}
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;
}
}
public 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
Field examples
static and instance
public class Variable public class Exercise
{ {
public static int i = 10; static void Main()
public int j = 1; {
public void test() Variable x = new
{ Variable();
i=i+10; x.test();
j=j+1; Variable y = new
Console.WriteLine(i); Variable();
Console.WriteLine(j); y.test();
} Console.ReadKey();
} }
}
• Output is:
20
2
30
2
Properties

• Client side looks like a field


• Internally behaves like method calls to get or
set the field (i.e. more abstract than a field)

• 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]
• http://www.dotnetperls.com/property
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)
• Internal
• Intuitively, accessible only within this program (more specific definition here)
• The default, but you should generally pick public or private instead

• Static
• Belongs to the type
• Instance
• Belongs to the instance
Access modifier error example

interface GameObject // no access modifier, defaults to internal


{
void Draw();
void Update();
}

public class Monster : GameObject


{
// ... implementations of Draw() and Update() go here ...
}

• Cannot have a base class (including interfaces) class less


accessible than a concrete class that derives from it
Conversion
Conversion operators

• Can also specify user-defined explicit and


implicit conversions
public 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)
{ Note n = (Note)442.578;
return ...; double d = n;
}
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();
}
VS 2015 and
MonoGame demo

You might also like