JL

C# Fundamentals – Arrays, Collections, Classes & OOP

Arrays

  • Two fundamental categories

    • Single-dimensional arrays

    • Linear collection stored in one dimension.

    • Accessed with a single index: arr[index].

    • Multi-dimensional arrays

    • Elements arranged in more than one dimension (rows, columns, depth).

    • Typical forms: 2-D and 3-D.

    • Visualized as grids/tables; accessed with multiple indices: matrix[row, column].

Collections

  • General characteristics

    • Implemented as classes in C#, that help us to store, manage and manipulate groups of related objects.

    • Grow or shrink dynamically (unlike fixed-size arrays).

    • Provide rich APIs for search, insertion, deletion, ordering, etc.

List
  • Use cases

    • Need a dynamic collection that changes size often.

    • Require ordered storage with index access list[i].

    • Store multiple elements of the same type.

Dictionary
  • Use cases

    • Need to associate unique keys with related values.

    • Fast retrieval when key is known.

    • Example: student ID → student record.

HashSet
  • Core idea: store only unique elements (duplicates automatically rejected).

  • No guarantee of element order.

  • Internally relies on hash codes for fast operations.

  • Common set operations (all mutate the current set):

    • UnionWith(collection)

    • Combines both sets (mathematical ∪).

    • IntersectWith(collection)

    • Keeps only common elements (∩).

    • ExceptWith(collection)

    • Removes all elements that exist in the second set ().

  • Preferred when

    • Uniqueness must be enforced.

    • Extremely fast existence checks are needed.

    • Ordering is irrelevant.

Classes & Objects

  • Class

    • Blueprint that encapsulates data and behavior.

    • Groups related fields, properties, and methods, making our code more organized and easier to understand.

  • Properties

    • Special members that provide a way to access and manipulate the data stored in private fields via get / set accessors.

    • Allow validation, calculations, or custom logic on read(getting)/write(setting) the value of a field.

  • Constructors

    • Special methods called during object creation.

    • Initialize object’s state with the necessary data.

    • Its name should be the same as the class name.

  • Methods

    • Define the actions/logic a class can perform.

    • Encapsulate the logic and behavior of the class.

    • May accept parameters and return values.

  • Objects (instances)

    • Concrete realizations of a class, each with its own state(data).

Object-Oriented Programming (OOP) Principles

Encapsulation
  • Bundle data and the methods, that operate on that data, into one unit or object while restricting direct access to some of an object’s components.

  • Keeps the data and code safe from outside interference.

  • Typical pattern:

    • private fields → hidden.

    • public property with private set → controlled modification.

    • public methods contain inner validation logic.

Inheritance
  • Create new (derived) classes from existing (base) classes → code reuse.

  • Keywords & concepts

    • virtual  : base class method that may be overridden.

    • override  : derived class provides a new implementation or extend the behavior of the base class method.

    • base   : used to sent data to base class.

Polymorphism
  • Treat objects of different classes as instances of a common base/interface.

  • Two forms

    • Compile-time (static) polymorphismmethod overloading (same method name, different parameter lists within the same class).

    • Run-time (dynamic) polymorphismmethod overriding (derived class provides a specific implementation of a method already defined in its base class) :

    • Derived class replaces an inherited virtual/abstract method.

Abstraction
  • Expose only essential features; hide implementation details.

  • It allows us to define classes, methods, and properties that focus on what an object does rather than how it does it.

  • Achieved via abstract classes and interfaces.

    • Abstract class

    • Cannot be instantiated directly.

    • May include abstract members (no body) and concrete members (with body) that must be implemented by derived classes.

    • Example 1: abstract class Shape { public abstract double Area(); }

    • Example 2: Shape circle = new Circle();

    • Interface

    • Pure contract that classes must follow; no implementation at all.

    • All implementing classes must supply definitions.

    • Example usage: IMovable car = new Car();

  • Interfaces offer an even higher level of abstraction than abstract classes.

Class Types in C#

  • Standard class – default, most common.

  • Static class

    • Cannot be instantiated; new is illegal.

    • Contains only static members.

    • Ideal for utility/helper functionality (e.g., Math class) without requiring object creation.

  • Abstract class

    • Serves as a template for other classes.

    • Cannot be instantiated.

    • Must be inherited; may force child classes to implement certain members.

    • Useful for creating a common base class with share logic while enforcing specific implementation in derived classes.

  • Sealed class

    • Cannot be inherited further.

    • Used when class design is final or for possible performance benefits.

  • Partial class

    • Single class split across multiple files (we can have 2 separate classes with the same name).

    • Enhances organization of very large classes.

  • Nested class

    • A class declared inside another class.

    • Access syntax: Outer.Inner inner = new Outer.Inner();

    • Improves encapsulation by grouping related logic within the same context.

  • Anonymous class

    • Temporary types with read-only properties.

    • Common in LINQ queries for data grouping or projection.

  • Extension class

    • Static class containing extension methods (static methods with this keyword on first parameter).

    • Adds new functionality to existing types(classes) without modifying their code.

    • Example: We can create a static class with a static method which adds a IsPalindrome() method to the string type.

Advanced Class Concepts

  • Static members

    • Belong to the class itself, shared by all instances.

    • Accessed directly using the class name: ClassName.Member.

    • Great for constants, counters, or global helpers.

  • Access modifiers - ensure proper encapsulation and protect data.

    • public  : visible everywhere.

    • private  : visible only inside the declaring class (strongest encapsulation).

    • protected : visible in declaring class + derived classes. Useful when the base class wants to provide some functionality to its derived classes while keeping it hidden from others.

    • internal : visible within the same assembly/project. Useful when we want to limit access to certain functionality to other classes within the same project but hide it from external projects.

  • Constructor chaining

    • One constructor invokes another to avoid duplicated initialization.

    • Way to call one constructor from another within the same class or between base and derived classes.

    • Can chain within the same class (this(...)) or up the hierarchy (base(...)).

File Handling (System.IO.File)

WRITING:

  • File.WriteAllText(path, text)

    • Overwrites the entire file → previous contents are lost.

  • File.AppendAllText(path, text)

    • Appends new text after existing content → preserves old data.

Naming & Coding Best Practices

  • camelCase : variables, parameter names (e.g., itemCount).

  • PascalCase : methods, classes (e.g., CalculateTotal() / ShoppingCart).

  • UPPERCASEWITH_UNDERSCORES : constants (e.g., MAX_LIMIT, PI).

  • Consistency in naming dramatically increases code readability and maintainability.