0% found this document useful (0 votes)
19 views33 pages

01b-Object Oriented Programming in C#

The document provides an overview of Object-Oriented Programming in C# with a focus on syntax, class creation, access specifiers, properties, constructors, and methods. It discusses the differences between value types and reference types, string manipulation, and type conversion, including handling cultural differences in data representation. Additionally, it emphasizes the use of nullable types and the importance of the CultureInfo class for parsing and formatting data correctly.

Uploaded by

hoster20125
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
0% found this document useful (0 votes)
19 views33 pages

01b-Object Oriented Programming in C#

The document provides an overview of Object-Oriented Programming in C# with a focus on syntax, class creation, access specifiers, properties, constructors, and methods. It discusses the differences between value types and reference types, string manipulation, and type conversion, including handling cultural differences in data representation. Additionally, it emphasizes the use of nullable types and the importance of the CultureInfo class for parsing and formatting data correctly.

Uploaded by

hoster20125
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/ 33

TOOL DEVELOPMENT

OBJECT ORIENTED PROGRAMMING IN C#


A DIGITAL ARTS AND ENTERTAINMENT PRESENTATION
C# syntax – hands on
CREATE FOLDER ‘Model’

 Be sure to add to project, not solution!

.2

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
C# syntax – hands on
CREATE CLASS ‘Hero’ INSIDE Model FOLDER

.3

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
C# CLASS
BASICS
C# syntax
HEADERS & DESTRUCTORS
No Headers
 C# doesn’t use header files
 Why not?
 .NET is compiled to an intermediate language,
the assemblies contain all the information
about the classes and methods in it

No Destructors
 C# is managed,
 therefore the Garbage Collector (GC) handles all (de)allocations
 No need for a destructor

No need to use the -> operator, instead use the . operator
.5
(DOT) to access the members
3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
C# syntax
ACCESS SPECIFIERS
Because of the lack of headers, every class, method, field and
property must have an access specifier.

public  Can be accessed from anywhere, same as C++

 Can not be accessed from outside the class, same as


private C++
 Default access specifier for fields and properties
 Can not be accessed from outside the class,
protected
except by an inherited class, same as C++
 Can only be accessed by classes in the same
internal namespace (ex. Library only)
 Default access specifier for a class in C# programming .6

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
PROPERTIES
MEMBER VARIABLES & EXPOSURE
C# syntax
MEMBER VARIABLES?
 Convention: _lowerCamelCase
 private
 Initialized by default

 Convention: UpperCamelCase
 public
 used to expose fields

.8

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
C# syntax
AUTO IMPLEMENTED PROPERTY

 Convention: UpperCamelCase
 public
 auto generates (hidden) field

.9

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
C# syntrax
CONTROLLING INPUT / OUTPUT
 auto implemented property:

 full property:

.10

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
C# syntax
ACCESS A PROPERTY

.11

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
C# syntax
ACCESS A PROPERTY

.12

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
CONSTRUCTORS
PAREMETERS & CONSTRUCTOR OVERLOADING
c# basics
CONSTRUCTOR OVERLOADING
Hero hero = new Hero(“Billy“, 4);
println(hero.Health); //?

Hero myHero = new Hero(“Bob“,1, 13);


hero.MyNemises = “none! “; //not in ctor

Hero fastHero = new Hero(“Fast“,10)


{ MyNemises=“Bob“, Health=123 };
//object initializers:
//call existing constructor,
//add more properties between {},
//using name = value
.14

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
METHODS
REMEMBER: NO DESTRUCTOR
C# syntax
METHODS INHERITED FROM SYSTEM.OBJECT

.16

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
C# syntax - exercise
USING THE ‘Hero’ CLASS
 Print the Name of a Hero class instance:

 Print the whole object:

 ??

.17

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
C# syntax - exercise
PRINTING AN OBJECT
Every object inherits from (is a) System.Object
 Inherits the ToString() function
 This is automatically being called when printing an object

 default: namespace.classname
 override:

.18

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
WORKING WITH STRING TYPE
STRING TYPE IN C#
strings in c#
STRING CONCATENATION
 using the + operator:

 this creates a new string!!


 reason: immutable (state of an object doesn’t change after creation)
 !! Modifying/Building a lot of strings can cause performance issues !!
(Constant creation and Garbage collection)

 using string interpolation (> C# 6 or higher):

.20

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
strings in c#
USING THE STRING TYPE
Check out StringBuilder

Check out the System.String MSDN documentation page

You can iterate over a string

Find a lot of helpful static methods:


 string.Format(…)
 string.IsNullOrEmpty(…)
 string.IsNullOrWhiteSpace(…)
…
.21

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
VALUE TYPES VS REFERENCE TYPES
WHAT, WHEN, WHERE, HOW
C# basics
OBJECT TYPES IN C#
C# has pointers,
but you’ll almost never use them (only in unsafe mode)

No need to use the -> operator,


instead we use the . operator (DOT) to access the members

Object types in C#:


 Value Types
 Structs, enumerations, numerical types, …

 Reference Types
 Classes, arrays, string, ....
.23

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
object types in C#
VALUE TYPES
• Holds the data in its own memory allocation
• Always passed by value
• (Pass by reference? Use the ‘ref’ keyword)

int wholeNumber 13
double myNumber 3.141
5

.24

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
object types in C#
REFERENCE TYPES
Store a reference to the data (Sort of Managed Pointers)
Always passed by reference in most cases
 string > Immutable > reference is passed by value
 Eg.: content += “, hi DAE“;
To pass a reference type by value,
create a ‘deep’ copy/clone

“Hello
world”
String content Hero object
Hero myHero
“Hello world, hi
DAE” .25

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
C# object types
REFERENCE TYPES: CODE EXAMPLE

.26

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
C# object types
SO WHAT IF I WANT MY VALUE TYPE TO BE
NULL?
 Value types can never be null (default values)
int wholeNumber 0
double myNumber 0.0

 Solution: Nullable types


 int? number = null;
 int notNullable = number;
 int notNullable = number ?? 0;

 Nullable types: value or reference type??


 value type;
 struct with ‘HasValue’ boolean that indicates if there is a value

.27

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
C# CONVERSION AND PARSING
SWITCHING BETWEEN TYPES
c# conversion and parsing
STRING TO NUMBER
Static methods in very .NET numerical Type
 Parse
 string input = ...;
int nr = int.Parse(input);
double d = double.Parse(input);
 no check for valid input  wrong format = crash!

 TryParse (No check, can crash if the string has a wrong format)
 string input = ...;
int nr;
int.TryParse(input, out nr);
 checks for valid input (returns boolean)
double d;
bool ok = double.TryParse(input, out d); .29

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
c# conversion and parsing
WHAT ABOUT ‘CULTURAL’ DIFFERENCES?
Notation rules per region:
 Belgian double  1,234 (comma)
 English double  1.123 (point)
 Think about dates!!
 Small detail but it can break your application!!

 Solution: CultureInfo

.30

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
c# conversion and parsing
CULTUREINFO
Provides information about a specific culture (aka Locale)
 Notation Rules for different types (Numbers, Date, Time, …)
 PARSING Rules for different types

 Several .NET methods can take a CultureInfo object as arguments


 Determines the cultural interpretation of the data

 int.Parse(string, cultureInfo)
 Default behavior = PC Locale Settings
 Check & pick the correct CultureInfo for your data

.31

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
c# conversion and parsing
CULTUREINFO - CODE EXAMPLE
[Comma] is used as a decimal
separator in Dutch locale

[Point] is not used as a


numerical separator in Dutch
locale

[Comma] is used as a
thousands separator in English
locale
[Point] is used as a decimal
separator in English locale

.32
CultureInfo Codes: http://msdn.microsoft.com/en-us/goglobal/bb896001.aspx
3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY
c# conversion and parsing
CULTUREINFO – ONE THAT FITS ALL?
Easiest Solution > CultureInfo.InvariantCulture (Culture – Insensitive)
 For background IO operations,
 not for UI purposes

CultureInfo.Invariant
 Associated with the English language not with any country/region
 Commonly used for internal IO/Parsing/Conversion operations

.33

3D.1
TOOL DEV DIGITAL ARTS AND FRIES BOURY

You might also like