Professional Documents
Culture Documents
4 Pertemuan 4
4 Pertemuan 4
CLASS
1.
2.
3.
4.
5.
Sta&c
Fields
Sta&c
Classes
Nested
Classes
Array
Collec&on
STATIC FIELDS
public
string
FirstName;
public
static
int
NextId;
1.
Access Modier
2.
Data Type
3.
Field Name
4.
Keyword sta&c
STATIC FIELDS
class
Employee
{
public
Employee(string
FirstName,
string
LastName)
{
this.FirstName
=
FirstName;
this.LastName
=
LastName;
Id
=
NextId;
NextId++;
}
public
static
int
NextId;
public
int
Id
{
get;
set;
}
public
int
FirstName
{
get;
set;
}
public
int
LastName
{
get;
set;
}
public
int
Salary
{
get;
set;
}
}
STATIC FIELDS
class
Program
{
static
void
Main()
{
Employee.NextId
=
1000000;
Employee
emp1
=
New
Employee(Inigo,
Montoya);
Employee
emp2
=
New
Employee(Princess,
Buttercup);
Console.WriteLine({0}
{1}
{2},
emp1.FirstName,
emp1.LastName,
emp1.Id);
}
}
STATIC CLASSES
public
class
Employee
{
public
string
GetFullName()
{
return
FirstName
+
+
LastName;
}
}
public
static
class
SimpleMath
{
public
static
float
Square(float
x,
int
y)
{
return
Math.Pow(x,
y);
}
}
STATIC CLASSES
public
static
class
SimpleMath
{
public
static
float
Square(float
x,
int
y)
{
return
Math.Pow(x,
y);
}
}
class
Program
{
static
void
Main()
{
/*
Using
Static
Classes
*/
SimpleMath.Square(3,
2);
}
}
STATIC CLASSES
public
static
class
TemperatureConverter
{
public
static
double
CelsiusToFahrenheit(string
temperatureCelsius)
{
//
Convert
argument
to
double
for
calculations.
double
celsius
=
Double.Parse(temperatureCelsius);
//
Convert
Celsius
to
Fahrenheit.
double
fahrenheit
=
(celsius
*
9
/
5)
+
32;
return
fahrenheit;
}
public
static
double
FahrenheitToCelsius(string
temperatureFahrenheit)
{
//
Convert
argument
to
double
for
calculations.
double
fahrenheit
=
Double.Parse(temperatureFahrenheit);
//
Convert
Fahrenheit
to
Celsius.
double
celsius
=
(fahrenheit
-
32)
*
5
/
9;
return
celsius;
}
}
STATIC CLASSES
class
TestTemperatureConverter
{
static
void
Main()
{
Console.WriteLine("Please
select
the
convertor
direction");
Console.WriteLine("1.
From
Celsius
to
Fahrenheit.");
Console.WriteLine("2.
From
Fahrenheit
to
Celsius.");
Console.Write(":");
string
selection
=
Console.ReadLine();
double
F,
C
=
0;
STATIC CLASSES
switch
(selection)
{
case
"1":
Console.Write("Please
enter
the
Celsius
temperature:
");
F
=
TemperatureConverter.CelsiusToFahrenheit(Console.ReadLine());
Console.WriteLine("Temperature
in
Fahrenheit:
{0:F2}",
F);
break;
case
"2":
Console.Write("Please
enter
the
Fahrenheit
temperature:
");
C
=
TemperatureConverter.FahrenheitToCelsius(Console.ReadLine());
Console.WriteLine("Temperature
in
Celsius:
{0:F2}",
C);
break;
default:
Console.WriteLine("Please
select
a
convertor.");
break;
}
//
Keep
the
console
window
open
in
debug
mode.
Console.WriteLine("Press
any
key
to
exit.");
Console.ReadKey();
}
}
DEFINING ARRAYS
class
Program
{
static
void
Main()
{
int[]
numbers;
/*
not
int
numbers[]
*/
numbers
=
new
int[20];
int[]
oddNumbers
=
new
int[10];
}
}
INITIALIZING ARRAYS
class
Program
{
static
void
Main()
{
int[]
evenNumbers
=
new
int[3]
{2,
4,
6};
}
}
ACCESSING ARRAYS
class
Program
{
static
void
Main()
{
int[]
evenNumbers
=
new
int[3];
evenNumbers[0]
=
2;
evenNumbers[1]
=
4;
evenNumbers[2]
=
6;
Console.WriteLine(evenNumbers[0]);
Console.WriteLine(evenNumbers[1]);
Console.WriteLine(evenNumbers[2]);
}
}
ACCESSING ARRAYS
class
Program
{
static
void
Main()
{
int[]
evenNumbers
=
new
int[3];
evenNumbers[0]
=
2;
evenNumbers[1]
=
4;
evenNumbers[2]
=
6;
foreach(int
number
in
evenNumbers)
{
Console.WriteLine(number);
}
}
}
COLLECTION
1.
2.
3.
4.
List
ArrayList
Dic&onary
Hashtable
COLLECTION
LIST
class
Program
{
static
void
Main()
{
List<string>
list
=
new
List<string>();
list.Add(Cat);
list.Add(Dog);
foreach
(string
element
in
list)
{
Console.WriteLine(element);
}
}
}
Cat
Dog
COLLECTION
ARRAYLIST
class
Program
{
static
void
Main()
{
ArrayList
list
=
new
ArrayList();
list.Add(Cat);
list.Add(2);
list.Add(false);
foreach
(string
element
in
list)
{
Console.WriteLine(element);
}
}
}
Cat
2
false
COLLECTION
DICTIONARY
class
Program
{
static
void
Main()
{
Dictionary<string,
int>
dict
=
new
Dictionary<string,
int>();
dict.Add(Cat,
1);
dict.Add(Dog,
4);
foreach
(string
element
in
list)
{
Console.WriteLine(element);
}
}
}
1
4
COLLECTION
HASHTABLE
class
Program
{
static
void
Main()
{
Hashtable
table
=
new
Hashtable();
table[Cat]
=
1;
table[Dog]
=
4;
foreach
(string
element
in
list)
{
Console.WriteLine(element);
}
}
}
1
4
COLLECTION
INITIALIZE
class
Program
{
class
Cat
{
public
int
Age
{
get;
set;
}
public
string
Name
{
get;
set;
}
}
static
void
Main()
{
Cat
cat
=
new
Cat
{
Age
=
10,
Name
=
Fluffy
}
}
}
COLLECTION
INITIALIZE
class
Program
{
static
void
Main()
{
List<Cat>
cats
=
new
List<Cat>
{
new
Cat()
{
Name
=
Sylvester,
Age
=
8}
new
Cat()
{
Name
=
Whiskers,
Age
=
2}
new
Cat()
{
Name
=
Sasha,
Age
=
4}
}
foreach
(Cat
c
in
cats)
{
System.Console.WriteLine(c.Name);
}
}
}
Sylvester
Whiskers
Sasha
COLLECTION
INITIALIZE
class
Program
{
static
void
Main()
{
List<Cat>
cats
=
new
List<Cat>()
Cat
c1
=
new
Cat()
{
Name
=
Sylvester,
Age
=
8}
Cat
c2
=
new
Cat()
{
Name
=
Whiskers,
Age
=
2}
Cat
c3
=
new
Cat()
{
Name
=
Sasha,
Age
=
4}
cats.Add(c1);
cats.Add(c2);
cats.Add(c3);
foreach
(Cat
c
in
cats)
{
System.Console.WriteLine(c.Name);
}
Sylvester
Whiskers
Sasha