ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Notes on C#
BY: MATT BOGGUS
SOME MATERIAL BASED ON ROGER CRAWFIS¡¯ C#
SLIDES
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
C# language features
Namespaces
Classes
? Fields
? Properties
? Methods
? Events
Structs
Enums
Interfaces
? Methods
? Properties
? Events
Control Statements
? if, else, while, for, switch, foreach
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 ; REFERENCE TYPE
Reference type
Original instance of the object
can be modified during
execution of method bodies
STRUCT ; VALUE TYPE
Value type
A copy of the object is made
and operated on inside method
bodies
Many types in XNA and MonoGame are defined as structs (ex: Vector2 and Rectangle)
Can pass structs as reference type using ¡®ref¡¯ keyword
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
¡°Rigid¡± interface
? Functionality is explicitly defined
? Ex: OSU Component Library where interfaces include Javadoc comments
with methods¡¯ requires and ensures clauses (pre and post conditions)
¡°Flexible¡± 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();
Interface example
IDelete deletableObj = new ImageBox();
deletableObj.Delete();
deletableObj = new TextBox();
deletableObj.Delete();
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;
public abstract int Area();
}
public class Square : Shape
{
public int width;
public int height;
public override int Area()
{
return width * height;
}
public void MoveLeft()
{
x--;
}
}
Abstract class example
Methods marked as abstract have no
bodies and must be overridden
Methods marked as virtual have
bodies and may be overridden
See http://msdn.microsoft.com/en-
us/library/sf985hc5.aspx for a longer
example
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
Output is:
20
2
30
2
public class NumStore
{
public static int i = 10;
public int j = 1;
public void AddAndPrint()
{
i=i+10;
j=j+1;
Console.WriteLine(i);
Console.WriteLine(j);
}
}
public class Exercise
{
static void Main()
{
NumStore x = new NumStore();
x.AddAndPrint();
NumStore y = new NumStore();
y.AddAndPrint();
Console.ReadKey();
}
}
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 ¨C syntax examples
type PropertyName { get; set; }
Many valid formats
int Score { get; set; }
string Name { get; }
double Time { get; private set; }
Code examples
? Person*.cs examples [Compare maintainability]
? 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
Access modifier error example
Cannot have an interface that is less accessible than a concrete class that
implements it (also applies to base and inheriting classes)
interface GameObject // no access modifier, defaults to internal
{
void Draw();
void Update();
}
public class Monster : GameObject
{
// ... implementations of Draw() and Update() go here ...
}
End of lecture
ADDITIONAL ON YOUR OWN SLIDES FOLLOW
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
On your own
Java to C# resources
The C# Programming Language for Java Developers ¨C documentation of
language differences organized by programming constructs
Additional Suggestions from StackOverflow
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 syntax example
How many methods should
classes/interfaces provide?
On your own references
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¡± ¨C 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
Conversion
Conversion operators
Can also specify user-defined explicit and implicit conversions
public class Note
{
private int value;
// Convert to hertz ¨C 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)442.578;
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();
}

More Related Content

Similar to CSharp presentation and software developement (20)

C# for C++ programmers
C# for C++ programmersC# for C++ programmers
C# for C++ programmers
Mark Whitaker
?
core java
core javacore java
core java
Vinodh Kumar
?
Dart structured web apps
Dart   structured web appsDart   structured web apps
Dart structured web apps
chrisbuckett
?
Advanced c#
Advanced c#Advanced c#
Advanced c#
saranuru
?
Writing code that writes code - Nguyen Luong
Writing code that writes code - Nguyen LuongWriting code that writes code - Nguyen Luong
Writing code that writes code - Nguyen Luong
Vu Huy
?
TechkTalk #12 Grokking: Writing code that writes code ¨C Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code ¨C Nguyen LuongTechkTalk #12 Grokking: Writing code that writes code ¨C Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code ¨C Nguyen Luong
Grokking VN
?
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
David McCarter
?
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
farhan amjad
?
Better Understanding OOP using C#
Better Understanding OOP using C#Better Understanding OOP using C#
Better Understanding OOP using C#
Chandan Gupta Bhagat
?
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
Jamie (Taka) Wang
?
Object Oriented Programming with Object Orinted Concepts
Object Oriented Programming with Object Orinted ConceptsObject Oriented Programming with Object Orinted Concepts
Object Oriented Programming with Object Orinted Concepts
farhanghafoor5
?
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
yazad dumasia
?
Dart, unicorns and rainbows
Dart, unicorns and rainbowsDart, unicorns and rainbows
Dart, unicorns and rainbows
chrisbuckett
?
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
Intro C# Book
?
Framework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users GroupFramework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users Group
brada
?
Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdf
AdilAijaz3
?
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8
Abou Bakr Ashraf
?
Clean Code 2
Clean Code 2Clean Code 2
Clean Code 2
Fredrik Wendt
?
C++ language
C++ languageC++ language
C++ language
Elizabeth Pisarek
?
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
Michael Stal
?
Dart structured web apps
Dart   structured web appsDart   structured web apps
Dart structured web apps
chrisbuckett
?
Writing code that writes code - Nguyen Luong
Writing code that writes code - Nguyen LuongWriting code that writes code - Nguyen Luong
Writing code that writes code - Nguyen Luong
Vu Huy
?
TechkTalk #12 Grokking: Writing code that writes code ¨C Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code ¨C Nguyen LuongTechkTalk #12 Grokking: Writing code that writes code ¨C Nguyen Luong
TechkTalk #12 Grokking: Writing code that writes code ¨C Nguyen Luong
Grokking VN
?
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
David McCarter
?
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
farhan amjad
?
Object Oriented Programming with Object Orinted Concepts
Object Oriented Programming with Object Orinted ConceptsObject Oriented Programming with Object Orinted Concepts
Object Oriented Programming with Object Orinted Concepts
farhanghafoor5
?
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
yazad dumasia
?
Dart, unicorns and rainbows
Dart, unicorns and rainbowsDart, unicorns and rainbows
Dart, unicorns and rainbows
chrisbuckett
?
Framework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users GroupFramework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users Group
brada
?
Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdf
AdilAijaz3
?
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8
Abou Bakr Ashraf
?
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
Michael Stal
?

Recently uploaded (20)

Leadership Spectrum by Sonam Sherpa at GDG Kathmandu March Monthly Meetup
Leadership Spectrum by Sonam Sherpa at GDG Kathmandu March Monthly MeetupLeadership Spectrum by Sonam Sherpa at GDG Kathmandu March Monthly Meetup
Leadership Spectrum by Sonam Sherpa at GDG Kathmandu March Monthly Meetup
GDG Kathmandu
?
APAC Solutions Challenge Info Session.pdf
APAC Solutions Challenge Info Session.pdfAPAC Solutions Challenge Info Session.pdf
APAC Solutions Challenge Info Session.pdf
GDG on Campus Monash
?
Testing Tools for Accessibility Enhancement Part II.pptx
Testing Tools for Accessibility Enhancement Part II.pptxTesting Tools for Accessibility Enhancement Part II.pptx
Testing Tools for Accessibility Enhancement Part II.pptx
Julia Undeutsch
?
UiPath NY AI Series: Session 4: UiPath AutoPilot for Developers using Studio Web
UiPath NY AI Series: Session 4: UiPath AutoPilot for Developers using Studio WebUiPath NY AI Series: Session 4: UiPath AutoPilot for Developers using Studio Web
UiPath NY AI Series: Session 4: UiPath AutoPilot for Developers using Studio Web
DianaGray10
?
CIOs Speak Out - A Research Series by Jasper Colin
CIOs Speak Out - A Research Series by Jasper ColinCIOs Speak Out - A Research Series by Jasper Colin
CIOs Speak Out - A Research Series by Jasper Colin
Jasper Colin
?
AuthZEN The OpenID Connect of Authorization - Gartner IAM EMEA 2025
AuthZEN The OpenID Connect of Authorization - Gartner IAM EMEA 2025AuthZEN The OpenID Connect of Authorization - Gartner IAM EMEA 2025
AuthZEN The OpenID Connect of Authorization - Gartner IAM EMEA 2025
David Brossard
?
202408_JAWSPANKRATION_Introduction_of_Minaden.pdf
202408_JAWSPANKRATION_Introduction_of_Minaden.pdf202408_JAWSPANKRATION_Introduction_of_Minaden.pdf
202408_JAWSPANKRATION_Introduction_of_Minaden.pdf
NTTDOCOMO-ServiceInnovation
?
Getting the Best of TrueDEM ¨C April News & Updates
Getting the Best of TrueDEM ¨C April News & UpdatesGetting the Best of TrueDEM ¨C April News & Updates
Getting the Best of TrueDEM ¨C April News & Updates
panagenda
?
Scalable Multi-Agent AI with AutoGen by Udai
Scalable Multi-Agent AI with AutoGen by UdaiScalable Multi-Agent AI with AutoGen by Udai
Scalable Multi-Agent AI with AutoGen by Udai
Udaiappa Ramachandran
?
Network_Packet_Brokers_Presentation.pptx
Network_Packet_Brokers_Presentation.pptxNetwork_Packet_Brokers_Presentation.pptx
Network_Packet_Brokers_Presentation.pptx
Khushi Communications
?
Microsoft Digital Defense Report 2024 .pdf
Microsoft Digital Defense Report 2024 .pdfMicrosoft Digital Defense Report 2024 .pdf
Microsoft Digital Defense Report 2024 .pdf
Abhishek Agarwal
?
Research Data Management (RDM): the management of dat in the research process
Research Data Management (RDM): the management of dat in the research processResearch Data Management (RDM): the management of dat in the research process
Research Data Management (RDM): the management of dat in the research process
HeilaPienaar
?
Sugarlab AI: How Much Does an XXX AI Porn Generator Cost in 2025
Sugarlab AI: How Much Does an XXX AI Porn Generator Cost in 2025Sugarlab AI: How Much Does an XXX AI Porn Generator Cost in 2025
Sugarlab AI: How Much Does an XXX AI Porn Generator Cost in 2025
Sugarlab AI
?
Build Your Uber Clone App with Advanced Features
Build Your Uber Clone App with Advanced FeaturesBuild Your Uber Clone App with Advanced Features
Build Your Uber Clone App with Advanced Features
V3cube
?
Dragino¥×¥í¥À¥¯¥È¥«¥¿¥í¥° LoRaWAN NB-IoT LTE cat.M1ÉÌÆ·¥ê¥¹¥È
Dragino¥×¥í¥À¥¯¥È¥«¥¿¥í¥° LoRaWAN  NB-IoT  LTE cat.M1ÉÌÆ·¥ê¥¹¥ÈDragino¥×¥í¥À¥¯¥È¥«¥¿¥í¥° LoRaWAN  NB-IoT  LTE cat.M1ÉÌÆ·¥ê¥¹¥È
Dragino¥×¥í¥À¥¯¥È¥«¥¿¥í¥° LoRaWAN NB-IoT LTE cat.M1ÉÌÆ·¥ê¥¹¥È
CRI Japan, Inc.
?
State_of_AI_Transformation in Germany.pdf
State_of_AI_Transformation in Germany.pdfState_of_AI_Transformation in Germany.pdf
State_of_AI_Transformation in Germany.pdf
VaradRajanKrishna
?
Commit Conf 2025 Bitnami Charts with Kubescape
Commit Conf 2025 Bitnami Charts with KubescapeCommit Conf 2025 Bitnami Charts with Kubescape
Commit Conf 2025 Bitnami Charts with Kubescape
Alfredo Garc¨ªa Lavilla
?
The metaverse : A Digital Transformation
The metaverse : A Digital TransformationThe metaverse : A Digital Transformation
The metaverse : A Digital Transformation
matlotloatang03
?
Building High-Impact Teams Beyond the Product Triad.pdf
Building High-Impact Teams Beyond the Product Triad.pdfBuilding High-Impact Teams Beyond the Product Triad.pdf
Building High-Impact Teams Beyond the Product Triad.pdf
Rafael Burity
?
How Telemedicine App Development is Revolutionizing Virtual Care.pptx
How Telemedicine App Development is Revolutionizing Virtual Care.pptxHow Telemedicine App Development is Revolutionizing Virtual Care.pptx
How Telemedicine App Development is Revolutionizing Virtual Care.pptx
Dash Technologies Inc
?
Leadership Spectrum by Sonam Sherpa at GDG Kathmandu March Monthly Meetup
Leadership Spectrum by Sonam Sherpa at GDG Kathmandu March Monthly MeetupLeadership Spectrum by Sonam Sherpa at GDG Kathmandu March Monthly Meetup
Leadership Spectrum by Sonam Sherpa at GDG Kathmandu March Monthly Meetup
GDG Kathmandu
?
APAC Solutions Challenge Info Session.pdf
APAC Solutions Challenge Info Session.pdfAPAC Solutions Challenge Info Session.pdf
APAC Solutions Challenge Info Session.pdf
GDG on Campus Monash
?
Testing Tools for Accessibility Enhancement Part II.pptx
Testing Tools for Accessibility Enhancement Part II.pptxTesting Tools for Accessibility Enhancement Part II.pptx
Testing Tools for Accessibility Enhancement Part II.pptx
Julia Undeutsch
?
UiPath NY AI Series: Session 4: UiPath AutoPilot for Developers using Studio Web
UiPath NY AI Series: Session 4: UiPath AutoPilot for Developers using Studio WebUiPath NY AI Series: Session 4: UiPath AutoPilot for Developers using Studio Web
UiPath NY AI Series: Session 4: UiPath AutoPilot for Developers using Studio Web
DianaGray10
?
CIOs Speak Out - A Research Series by Jasper Colin
CIOs Speak Out - A Research Series by Jasper ColinCIOs Speak Out - A Research Series by Jasper Colin
CIOs Speak Out - A Research Series by Jasper Colin
Jasper Colin
?
AuthZEN The OpenID Connect of Authorization - Gartner IAM EMEA 2025
AuthZEN The OpenID Connect of Authorization - Gartner IAM EMEA 2025AuthZEN The OpenID Connect of Authorization - Gartner IAM EMEA 2025
AuthZEN The OpenID Connect of Authorization - Gartner IAM EMEA 2025
David Brossard
?
Getting the Best of TrueDEM ¨C April News & Updates
Getting the Best of TrueDEM ¨C April News & UpdatesGetting the Best of TrueDEM ¨C April News & Updates
Getting the Best of TrueDEM ¨C April News & Updates
panagenda
?
Scalable Multi-Agent AI with AutoGen by Udai
Scalable Multi-Agent AI with AutoGen by UdaiScalable Multi-Agent AI with AutoGen by Udai
Scalable Multi-Agent AI with AutoGen by Udai
Udaiappa Ramachandran
?
Microsoft Digital Defense Report 2024 .pdf
Microsoft Digital Defense Report 2024 .pdfMicrosoft Digital Defense Report 2024 .pdf
Microsoft Digital Defense Report 2024 .pdf
Abhishek Agarwal
?
Research Data Management (RDM): the management of dat in the research process
Research Data Management (RDM): the management of dat in the research processResearch Data Management (RDM): the management of dat in the research process
Research Data Management (RDM): the management of dat in the research process
HeilaPienaar
?
Sugarlab AI: How Much Does an XXX AI Porn Generator Cost in 2025
Sugarlab AI: How Much Does an XXX AI Porn Generator Cost in 2025Sugarlab AI: How Much Does an XXX AI Porn Generator Cost in 2025
Sugarlab AI: How Much Does an XXX AI Porn Generator Cost in 2025
Sugarlab AI
?
Build Your Uber Clone App with Advanced Features
Build Your Uber Clone App with Advanced FeaturesBuild Your Uber Clone App with Advanced Features
Build Your Uber Clone App with Advanced Features
V3cube
?
Dragino¥×¥í¥À¥¯¥È¥«¥¿¥í¥° LoRaWAN NB-IoT LTE cat.M1ÉÌÆ·¥ê¥¹¥È
Dragino¥×¥í¥À¥¯¥È¥«¥¿¥í¥° LoRaWAN  NB-IoT  LTE cat.M1ÉÌÆ·¥ê¥¹¥ÈDragino¥×¥í¥À¥¯¥È¥«¥¿¥í¥° LoRaWAN  NB-IoT  LTE cat.M1ÉÌÆ·¥ê¥¹¥È
Dragino¥×¥í¥À¥¯¥È¥«¥¿¥í¥° LoRaWAN NB-IoT LTE cat.M1ÉÌÆ·¥ê¥¹¥È
CRI Japan, Inc.
?
State_of_AI_Transformation in Germany.pdf
State_of_AI_Transformation in Germany.pdfState_of_AI_Transformation in Germany.pdf
State_of_AI_Transformation in Germany.pdf
VaradRajanKrishna
?
The metaverse : A Digital Transformation
The metaverse : A Digital TransformationThe metaverse : A Digital Transformation
The metaverse : A Digital Transformation
matlotloatang03
?
Building High-Impact Teams Beyond the Product Triad.pdf
Building High-Impact Teams Beyond the Product Triad.pdfBuilding High-Impact Teams Beyond the Product Triad.pdf
Building High-Impact Teams Beyond the Product Triad.pdf
Rafael Burity
?
How Telemedicine App Development is Revolutionizing Virtual Care.pptx
How Telemedicine App Development is Revolutionizing Virtual Care.pptxHow Telemedicine App Development is Revolutionizing Virtual Care.pptx
How Telemedicine App Development is Revolutionizing Virtual Care.pptx
Dash Technologies Inc
?

CSharp presentation and software developement

  • 1. Notes on C# BY: MATT BOGGUS SOME MATERIAL BASED ON ROGER CRAWFIS¡¯ C# SLIDES
  • 2. 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
  • 3. C# language features Namespaces Classes ? Fields ? Properties ? Methods ? Events Structs Enums Interfaces ? Methods ? Properties ? Events Control Statements ? if, else, while, for, switch, foreach
  • 5. 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
  • 6. Classes vs. structs CLASS ; REFERENCE TYPE Reference type Original instance of the object can be modified during execution of method bodies STRUCT ; VALUE TYPE Value type A copy of the object is made and operated on inside method bodies Many types in XNA and MonoGame are defined as structs (ex: Vector2 and Rectangle) Can pass structs as reference type using ¡®ref¡¯ keyword
  • 8. 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
  • 9. Interfaces ¡°Rigid¡± interface ? Functionality is explicitly defined ? Ex: OSU Component Library where interfaces include Javadoc comments with methods¡¯ requires and ensures clauses (pre and post conditions) ¡°Flexible¡± 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 }
  • 10. 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(); Interface example IDelete deletableObj = new ImageBox(); deletableObj.Delete(); deletableObj = new TextBox(); deletableObj.Delete();
  • 12. 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
  • 13. public abstract class Shape { protected int x = 50; protected int y = 50; public abstract int Area(); } public class Square : Shape { public int width; public int height; public override int Area() { return width * height; } public void MoveLeft() { x--; } } Abstract class example Methods marked as abstract have no bodies and must be overridden Methods marked as virtual have bodies and may be overridden See http://msdn.microsoft.com/en- us/library/sf985hc5.aspx for a longer example
  • 15. 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); } }
  • 16. 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; } }
  • 17. 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
  • 18. Field examples static and instance Output is: 20 2 30 2 public class NumStore { public static int i = 10; public int j = 1; public void AddAndPrint() { i=i+10; j=j+1; Console.WriteLine(i); Console.WriteLine(j); } } public class Exercise { static void Main() { NumStore x = new NumStore(); x.AddAndPrint(); NumStore y = new NumStore(); y.AddAndPrint(); Console.ReadKey(); } }
  • 19. 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
  • 20. Properties ¨C syntax examples type PropertyName { get; set; } Many valid formats int Score { get; set; } string Name { get; } double Time { get; private set; } Code examples ? Person*.cs examples [Compare maintainability] ? http://www.dotnetperls.com/property
  • 21. 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
  • 22. Access modifier error example Cannot have an interface that is less accessible than a concrete class that implements it (also applies to base and inheriting classes) interface GameObject // no access modifier, defaults to internal { void Draw(); void Update(); } public class Monster : GameObject { // ... implementations of Draw() and Update() go here ... }
  • 23. End of lecture ADDITIONAL ON YOUR OWN SLIDES FOLLOW
  • 24. 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
  • 25. On your own Java to C# resources The C# Programming Language for Java Developers ¨C documentation of language differences organized by programming constructs Additional Suggestions from StackOverflow
  • 26. 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 syntax example
  • 27. How many methods should classes/interfaces provide? On your own references 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¡± ¨C 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
  • 29. Conversion operators Can also specify user-defined explicit and implicit conversions public class Note { private int value; // Convert to hertz ¨C 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)442.578; double d = n;
  • 30. 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(); }
  • 31. 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(); }