CS计算机代考程序代写 data structure c# algorithm C# Classes

C# Classes

C# Programming

Quincy College

Fall 2021

Robert I. Pitts

 Program entry point
◦ Unit testing

 Learning class libraries

 Scope of variables

 Overloading vs. optional parameters

 Pass by reference

 Enumerations

Fall 2021C# Programming 2

 Class represents main program needs Main
method
◦ Code starts program: “entry point”

 If multiple classes with Main, tell IDE entry point

◦ Project > ProjectName Properties > Application >
Select class from Startup object: list box

 Use Main in other classes for unit testing

◦ However, since method of class, has access to
private data

Fall 2021C# Programming 3

 IndexOf, Substring
◦ Find/extract characters

 Comparison (==, !=)

 Concatenation (+, +=)
◦ Simple types converted
◦ Object’s ToString method called

 Verbatim string literal (escaping not needed)
@”C:\Users\LiJin\Desktop”

◦ Include newline by typing Enter

 Interpolate variable values with $”…{var} …”

Fall 2021C# Programming 4

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated

 Method, property, or field known in entire class
(order irrelevant)

 Instance variables
◦ Initialized to default value for

data type if not in constructor
◦ Default for references is null

 Local variables only known in method
◦ Must be given value before used

 Inner block variable hides variable with same
name in outer blocks

Fall 2021C# Programming 5

 Methods in class must be distinguishable by
signature
◦ Signature: method name and number, type, and

order of parameters (not return type)

◦ May overload with same name, so long as
parameters differ

 Allows initializing objects with various constructors
new DateTime(year, month, day)

new DateTime(year, month, day,

JapaneseCalendar)

Fall 2021C# Programming 6

 May also make parameters optional by specifying
default values
string DayOfWeek(int year,

int month = 1,
int day = 1)


DayOfWeek(2021, 9, 1)

DayOfWeek(2021, 10)

DayOfWeek(2021)

 Named parameters: alternate way to pass
DayOfWeek(day: 5, year: 2021)

◦ Allows skipping intermediate defaults

Fall 2021C# Programming 7

 By default, copies what passed and
returned

 Reference types alterable by method
if class allows changes (mutable)

 Promotion rules: converting one
simple type to another

 Simple types convertible to object

◦ Example with Console.Write

Fall 2021C# Programming 8

25

copy

25

25

http://msdn.microsoft.com/en-us/library/system.console.write(v=vs.110).aspx

 Can alter parameter value pass in as ref
void Penalize(ref int val1, ref int val2)

{

val1–;

val2–;

}

int hw1 = 12, bonus1 = 2;

Penalize(ref hw1, ref bonus1);

 ref parameter must have value before call

 Works for value and reference types

Fall 2021C# Programming 9

ref keyword used with
both formal and actual
parameters

 Method may send value out

void ExtractParts(string name,
out string fName,
out string lName)

{
int spacePos = name.IndexOf(‘ ‘);
fName = name.Substring(0, spacePos);
lName = name.Substring(spacePos + 1);

}

string first, last;

ExtractParts(“Ann Doe”,out first,out last);

 out parameter must be given value in method

Fall 2021C# Programming 10

 Specify sequence of constant values
enum Level {

Beginner, Intermediate, Advanced };

Level gameLevel = Level.Beginner;

◦ Constant values start at zero, but may give explicit
values: { Beginner = 1, …}

 Only assign/compare to value of same enum type
(unless cast)
int screenNum = (int)gameLevel;

Fall 2021C# Programming 11

 Unit testing can be achieved with a Main
method in each class

 May reuse method name with overloading and
default arguments

 Parameters passed to methods are copies
◦ Can alter object via its copied reference if mutable

◦ May also pass by reference using ref or out

 Enumeration provides constants with type
checking

Fall 2021C# Programming 12

 static variables and methods

 Math library class

 Casting

 Random number generation

 Recursion (Data Structures and Algorithms topic)

Fall 2021C# Programming 13