C Sharp
Statements are instructions, they end with a ;
Notes:
=
is used for assignment==
is used for equality!=
is used for inequality (! - bang)&&
(and) both sides must be true||
(or) either part must be true+=
increment & update valuex++
- Increment by 1y = ++x- increments x then assigns value.y = x++
- assigns value, then increments.
x--
- Decrement by 1
Bugs:
- Compile Time Bugs
- Runtime Bugs
Variables:
- Always have a type, a name, & a value that can change.
Types:
int
- Interger (1)double
- 2 points (1.00...)string
- string ("Hello World")var
- variable (inferred)
Interpolation:
Console.WriteLine($"My Age: {myAge}, x: {x}, Hourly Rate: {hourlyRate}, MyName:{myName}");
Constants:
assigned a value that never changes
enum
An array of constants accessed by . (myEnum.uno)
Switch Statements:
Classes:
public class Employee
{
// user defined type
public enum Rating
{
poor,
good,
excellent
}
// var rating is a Rating
private Rating rating;
// prop
public double Income { get; set; }
public int YearsOfService { get; set; }
// method
public void SetRating(Rating rating)
{
this.rating = rating;
}
public void CalcRaise()
{
double baseRaise = Income * .05;
double bonus = YearsOfService * 1000;
Income += baseRaise + bonus;
switch (rating)
{
case Rating.poor:
Income -= YearsOfService * 2000;
break;
case Rating.good:
break;
case Rating.excellent:
Income += YearsOfService * 500;
break;
}
Console.WriteLine($"New income is {income}")
}
}
// An instance of a class is called an object (instantiate)
Employee joe = new Employee();
- Methods:
- functions assigned to a class
- public - can be seen by any other method
- private - can only be seen by methods of the same class
- Properties are, by their nature, public.
- Fields, by their nature, are private to a class. (can only be accessed by methods of the class)
Casing:
- camelCase
- fields, variables, & parameters
- PascalCase
- Classes, costants, & properties
Properties:
// Full
private string name;
public string Name{
get { return name; }
set { name = value; }
}
// Automatic
public string Name { get; set; }
// Read Only
public string Name { get; }
// Read Only With Initialization
public string Name { get; } = "Clay Dunston";
Method Overloading: