JavaScript required
We’re sorry, but Coda doesn’t work properly without JavaScript enabled.
Skip to content
Gallery
Study Notes(Edit)
Resources
Networks
CAB201
Binary Calculator
Color Palette Finder
Database Management
More
Share
Explore
Exam Notes
Week 5: Classes and Objects in C#
1. Classes and Objects
Class
: A blueprint for creating objects, containing fields, properties, and methods.
Syntax
:
csharp
Copy code
public class ClassName
{
// Members (fields, properties, methods)
}
Object
: An instance of a class, created using the
new
keyword.
Example
:
csharp
Copy code
ClassName obj = new ClassName();
2. Access Modifiers
Control the accessibility of class members:
Table 17
Table 17
Modifier
Accessibility
Modifier
Accessibility
1
public
Accessible from any code
2
internal
Accessible only within the same assembly
3
protected
Accessible within the class and its derived classes
4
private
Accessible only within the class itself
There are no rows in this table
3. Constructors
Constructor
: A special method to initialize objects, sharing the class name and having no return type.
Default Constructor
: Automatically provided if no other constructor is defined; initializes fields to default values.
Parameterized Constructor
: Allows setting initial values for fields.
Overloaded Constructors
: Multiple constructors with different parameter lists for flexibility.
Calling Base Constructors
: Use
base
to call a constructor from a parent class.
Syntax
:
csharp
Copy code
public ChildClass(int value) : base(value) { }
4. Fields and Properties
Fields
Variables within a class to store the object’s state.
Syntax
:
csharp
Copy code
private int _age;
Properties
Use
get
and
set
to provide controlled access to fields, often including validation logic.
Auto-Implemented Properties
:
Syntax
:
csharp
Copy code
public int Age { get; set; }
5. Method Overloading
Method Overloading
: Allows methods with the same name but different parameter lists.
Example
:
csharp
Copy code
public void Print(string text) { }
public void Print(int number) { }
Purpose
: Provides flexibility to use similar methods with different inputs.
6. Class Relationships
Composition
A "part-of" relationship where one class owns another. If the container object is destroyed, so is the contained object.
Example
:
csharp
Copy code
class Car { Engine engine = new Engine(); }
Aggregation
A "has-a" relationship where one class references another without controlling its lifecycle.
Example
:
csharp
Copy code
class Team { Player player; } // Team uses but doesn’t control Player
Association
A "uses-a" relationship between two independent classes without ownership.
Example
:
csharp
Copy code
class Teacher { void Teach(Student student) { } }
7. Static Members
Static Fields and Methods
: Shared among all instances; belong to the class itself, not individual objects.
Syntax
:
csharp
Copy code
public static int count;
✨ Quick Study Tips
Understand Access Modifiers
: Practice where each modifier applies, especially
protected
and
internal
.
Review Constructors and
base
Keyword
: Know how constructors initialize objects and how derived classes can call parent constructors.
Differentiate Relationships
: Composition vs. Aggregation, where lifecycle management is key in understanding the difference.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
Ctrl
P
) instead.