CSharp and Software Design
CSharp and Software Design
design and C#
Outline
• Simple
• Readable
• Maintainable
• Reusable
• 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
{ {
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
• Reuse of existing
code in other
projects
• Related software
design principle
• Composition over inh
eritance
• 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
• Heap-based allocation
• Use “new” keyword to instantiate
• Automatic garbage collection
Java to C# resources
Class Struct
• Reference type • Value type
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
• 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() { ... }
}
• 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
• 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
• 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