C# Learning Outline - Full Textbook
Style
Chapter 3: Object-Oriented Programming in C#
3.7 Understanding Properties
In C#, properties provide a flexible mechanism to read, write, or compute the value of a
private field. Properties are often used to protect a field from being accessed directly while
still allowing external code to read and write values in a controlled manner.
Properties are defined with get and set accessors:
- Get Accessor: Used to return the property value.
- Set Accessor: Used to assign a new value to the property.
Defining Properties
class Person
{
private string name; // Private field
// Property to get and set the value of 'name'
public string Name
{
get { return name; }
set { name = value; }
}
}
In this example:
- The name field is private, meaning it cannot be accessed directly.
- The Name property is public, providing a controlled way to access the name field using
get and set methods.
Auto-Implemented Properties
class Person
{
public string Name { get; set; } // Auto-implemented property
public int Age { get; private set; } // Read-only property
}
Read-Only Properties
class Car
{
public string Model { get; }
public string Make { get; }
// Constructor initializes read-only properties
public Car(string model, string make)
{
Model = model;
Make = make;
}
}
Computed Properties
class Rectangle
{
public double Width { get; set; }
public double Height { get; set; }
// Computed property
public double Area
{
get { return Width * Height; }
}
}
3.8 Static Members
Static members of a class belong to the class itself rather than to any specific instance. Static
members are shared across all instances of a class.
Static Fields
class Employee
{
public static int employeeCount = 0;
public Employee()
{
employeeCount++; // Increment static field for each new object
}
}
Static Methods
class MathUtility
{
public static int Add(int a, int b)
{
return a + b;
}
}
3.9 Namespaces
Namespaces in C# are used to organize code and avoid name conflicts. A namespace
provides a way to group related classes, interfaces, and other types.
Defining a Namespace
namespace Company.Project
{
class Employee
{
public string Name { get; set; }
}
}
3.10 Structs vs. Classes
In C#, structs are similar to classes, but they are value types, whereas classes are reference
types.
Differences Between Structs and Classes:
- Structs are value types.
- Classes are reference types.
struct Point
{
public int X;
public int Y;
public Point(int x, int y)
{
X = x;
Y = y;
}
}
Point point1 = new Point(10, 20);
Point point2 = point1; // A copy is made
point2.X = 30;
Console.WriteLine(point1.X); // Output: 10 (original remains unchanged)
3.11 Enumerations (Enums)
An enum is a special "class" that represents a group of constants (unchangeable/read-only
variables). It’s used to define a set of named integral constants that represent a type of data
that does not change.
Example of an enum:
enum DayOfWeek
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
DayOfWeek today = DayOfWeek.Wednesday;
Exercises
Exercise 1: Static Members
Create a Book class with a static field bookCount that keeps track of how many Book objects
are created. Each time a new Book is instantiated, increase the bookCount by one.
Exercise 2: Working with Properties
Create a Student class with properties for Name, Age, and Grade. Use auto-implemented
properties for Name and Grade, and create a read-only property for Age that is set through
the constructor.