C# Programming Fundamentals Cheat Sheet

This cheat sheet covers core C# concepts including object-oriented programming with classes and inheritance, LINQ for data manipulation, and basic UI element interaction within a Windows Forms application.

Core Principles

  • Object-Oriented Programming (OOP): Utilize classes (e.g., `Person`, `Employee`) to model real-world entities, encapsulating data (fields) and behavior (methods).
  • Inheritance: Extend base classes (e.g., `Employee`) with derived classes (e.g., `Manager`, `Developer`) to share common properties and implement specific behaviors.
  • Abstract Classes: Define a common interface for derived classes using abstract methods (e.g., `work()` in `Employee`) that must be implemented by subclasses.
  • LINQ (Language Integrated Query): Employ LINQ methods like `Where` and `ToList` for efficient data filtering and manipulation of collections.
  • Event Handling: Implement event handlers (e.g., `button1_Click`) to respond to user interactions within a GUI.

Action Steps

  • 1. Define a class with private fields and public properties for data encapsulation (Task 1.1 & 1.2).
  • 2. Filter a list of objects based on specific criteria using LINQ's `Where` method (Task 1.3).
  • 3. Iterate through a filtered list to display relevant information (Task 1.4).
  • 4. Apply multiple LINQ `Where` conditions sequentially to refine data selection (Task 1.5).
  • 5. Clear list box items programmatically (Task 1.7).
  • 6. Create an abstract base class with common properties and abstract methods (Task 2.1-2.3).
  • 7. Implement derived classes that inherit from an abstract base class and override abstract methods (Task 2.4-2.7).
  • 8. Dynamically add columns to a DataGridView (Task 2.8).
  • 9. Populate a DataGridView by iterating through a list of objects and adding rows (Task 2.9).
  • 10. Generate a list of unique random perfect squares within a given range (Task 3.1).
  • 11. Filter a list of numbers into different categories based on their values (Task 3.2).
  • 12. Display filtered data in UI labels, separated by commas (Task 3.3).
  • 13. Change the foreground color of UI labels to visually distinguish data categories (Task 3.4).

Key Terms

  • Class: A blueprint for creating objects, defining properties and methods.
  • Object: An instance of a class, with its own state and behavior.
  • Property: A member of a class that provides a flexible mechanism to read, write, or compute the value of a private field.
  • Abstract Class: A class that cannot be instantiated and may contain abstract methods that must be implemented by derived classes.
  • Inheritance: A mechanism where a new class derives properties and methods from an existing class.
  • LINQ: Language Integrated Query, a set of technologies that add native data querying capabilities to .NET languages.
  • Event Handler: A method that is called when a specific event occurs, such as a button click.
  • Perfect Square: An integer that is the square of an integer (e.g., 9 is a perfect square because 3*3=9).

Pro Tips

  • Use `string.Join` for efficient concatenation of list items into a single string for display.
  • Leverage `HashSet<int>` to automatically ensure uniqueness when generating random numbers.
  • Employ `MessageBox.Show` for quick debugging or displaying summary information to the user.

Pitfalls to Avoid

  • Forgetting to call `.ToList()` after a LINQ `Where` clause can lead to deferred execution issues.
  • Not clearing UI elements (like ListBoxes) before repopulating them can result in duplicate data display.
  • Incorrectly defining the range in `random.Next(min, max + 1)` can exclude the upper bound.

Myth vs Reality

  • All numbers generated by `random.Next(min, max)` include `max`.: The `max` parameter in `random.Next(min, max)` is exclusive; to include `max`, use `random.Next(min, max + 1)`.
  • LINQ queries execute immediately when defined.: LINQ queries often use deferred execution; they execute only when the results are iterated upon (e.g., by `ToList()` or a `foreach` loop).

Real World Examples

  • Filtering a list of people to find those with last names starting with 'D'.: Used in Task 1.3 to select `Person` objects where `LastName` begins with 'D'.
  • Filtering employees based on salary range.: Used in Task 2.9 to count employees earning 50000 or more.
  • Categorizing generated perfect squares into ranges.: Used in Task 3.2 to group squares into <=100, 101-200, and >200 categories.

Statistics

  • Number of people with last names starting with 'D': 2
  • Number of people aged between 26 and 50 (inclusive): 2
  • Number of employees with salary >= 50000: 10
  • Number of perfect squares generated (target): 15
  • Range for random number generation (min): 1
  • Range for random number generation (max): 300

Timeline

  • Initialization: Form1 constructor runs, initializing lists of `Person` and `Employee` objects, and setting up DataGridView columns.
  • Button 1 Click (Task 1.3): Filters `Person` list by last name starting with 'D' and displays results in `listBox1`.
  • Button 2 Click (Task 1.5): Filters `Person` list by age (26-50) and displays results in `listBox2`.
  • Button 3 Click (Task 1.7): Clears both `listBox1` and `listBox2`.
  • Form1 Constructor (Task 2.9): Populates `dataGridView1` with `Employee` data and counts employees earning >= 50000, displaying the count via `MessageBox`.
  • Button 1 Click (Task 3.1): Generates 15 unique random perfect squares between 1 and 300.
  • Button 1 Click (Task 3.2 & 3.3): Filters generated squares into three ranges (<=100, 101-200, >200) and displays them in `label1`, `label2`, and `label3`.
  • Button 1 Click (Task 3.4): Sets the foreground color of `label1` to Red, `label2` to Green, and `label3` to Blue.

People

  • Alice Brown: Person (Age 17)
  • Ben Dawkins: Person (Age 21)
  • Clara Dawson: Person (Age 34)
  • George Miller: Person (Age 65)
  • Helen Turner: Person (Age 72)
  • John Smith: Manager (Salary 75000)
  • Sarah Johnson: Manager (Salary 82000)
  • Michael Brown: Manager (Salary 95000)
  • Emily Davis: Manager (Salary 68000)
  • David Wilson: Manager (Salary 88000)
  • Alex Chen: Developer (Salary 65000)
  • Jessica Martinez: Developer (Salary 72000)
  • Ryan Thompson: Developer (Salary 58000)
  • Olivia Garcia: Developer (Salary 78000)
  • Daniel Rodriguez: Developer (Salary 52000)

Related cheat sheets

  • Java Object-Oriented Programming Cheat Sheet
  • C# Fundamentals: OOP, LINQ & UI Interaction
  • Advanced Factoring Techniques Cheat Sheet
  • Ensemble Modeling Cheat Sheet

More like this

  • Coding cheat sheets
  • Explore all cheat sheets

ClipSheet — AI Cheat Sheet Generator

ClipSheet transforms YouTube videos, PDFs, and text into structured cheat sheets and study notes using AI. Built for students, professionals, and content creators who need to learn faster.

Features

  • AI-powered extraction of key concepts, formulas, and action steps
  • Automatic quiz and flashcard generation for active recall
  • PDF export and public sharing via unique URLs
  • Support for YouTube videos, PDFs, and raw text input

Browse by Category

  • All Cheat Sheets
  • Science & Academic
  • Technology
  • Health & Fitness
  • Coding
  • Business
  • Education
  • Productivity
  • Finance
  • Lifestyle

Legal

  • Privacy Policy
  • Terms of Service
  • Imprint