← Back to quizzesFree quiz

Fundamentals of C# Language

Welcome to this comprehensive guide on the core concepts of the C# programming language. Whether you are a beginner or brushing up on fundamentals, this course will walk you through…

10 questions~5 min
Fundamentals of C# Language — Qwi
0 / 10
Score: 0%
1

Which access modifier makes a member visible only to derived classes and the same assembly?

2

In C#, what is the result of casting a double to int using (int) d?

3

Which C# feature allows a class to define a property with custom get and set logic?

4

When overriding a virtual method in a derived class, which keyword must be used?

5

What does the 'static' keyword indicate when applied to a class member?

6

Which collection interface must a class implement to be usable with a foreach loop?

7

What is the purpose of the 'using' directive in a C# source file?

8

Which of the following statements about C# structs is true?

9

What does the 'ref' keyword indicate for a method parameter in C#?

10

Which C# feature enables a class to expose an event that other classes can subscribe to?

Fundamentals of C# Language

Welcome to this comprehensive guide on the core concepts of the C# programming language. Whether you are a beginner or brushing up on fundamentals, this course will walk you through essential topics such as access modifiers, type casting, properties, inheritance, static members, collections, namespaces, and structs. Each section is designed to be SEO‑friendly, using clear headings, keyword‑rich paragraphs, and well‑structured lists to help both learners and search engines understand the material.

1. Understanding Access Modifiers

Access modifiers control the visibility of types and members in C#. The most common modifiers are public, private, internal, and protected. Among these, protected is unique because it restricts access to the declaring class and any class that derives from it, while still allowing access within the same assembly when combined with internal (i.e., protected internal).

  • public: No restrictions; accessible from any code.
  • private: Accessible only within the containing class.
  • internal: Accessible only within the same assembly.
  • protected: Accessible within the declaring class and its derived classes.

Quiz Insight: The question "Which access modifier makes a member visible only to derived classes and the same assembly?" highlights the role of protected (and its combination with internal for broader visibility).

2. Casting Between Numeric Types

Casting is a fundamental operation when converting values from one type to another. In C#, casting a double to an int using the syntax (int) d truncates the fractional part rather than rounding. This behavior is important for performance‑critical code where explicit control over rounding is required.

  • Example: double d = 3.9; int i = (int)d; // i becomes 3
  • If rounding is needed, use Math.Round before casting.

Quiz Insight: The correct answer "The fractional part is truncated" reinforces the need to understand implicit data loss during narrowing conversions.

3. Properties: Encapsulating Get and Set Logic

Properties provide a clean way to expose class data while allowing custom logic for getting and setting values. Unlike fields, properties can enforce validation, lazy loading, or raise events when a value changes.

public class Person
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentException("Name cannot be empty");
            _name = value;
        }
    }
}

Key points:

  • Properties are declared with get and set accessors.
  • They can be read‑only (get only) or write‑only (set only).
  • Auto‑implemented properties (public int Age { get; set; }) simplify syntax when no extra logic is needed.

Quiz Insight: The question about "Which C# feature allows a class to define a property with custom get and set logic?" directly points to Properties.

4. Overriding Virtual Methods

Inheritance enables code reuse and polymorphism. To customize behavior in a derived class, you override a base class's virtual method using the override keyword. The base method must be marked virtual, abstract, or override for this to work.

public class Animal
{
    public virtual void Speak()
    {
        Console.WriteLine("Animal sound");
    }
}

public class Dog : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Woof!");
    }
}

Important notes:

  • Use sealed on an overriding method to prevent further overrides.
  • The new keyword hides a base member without polymorphic behavior.

Quiz Insight: The correct answer "override" emphasizes the syntax required for method overriding.

5. The Meaning of the static Keyword

When a member is declared static, it belongs to the type itself rather than any particular instance. This is useful for utility functions, shared state, or constants.

  • Static fields hold data common to all instances.
  • Static methods can be called without creating an object (Math.Sqrt(9)).
  • Static classes (public static class Helper) cannot be instantiated and can contain only static members.

Example:

public class Counter
{
    private static int _totalCount;
    public Counter()
    {
        _totalCount++;
    }
    public static int TotalCount => _totalCount;
}

Quiz Insight: The statement "The member belongs to the class itself, not to any instance" captures the essence of static members.

6. Collections and the IEnumerable Interface

To enable iteration with foreach, a class must implement IEnumerable (or the generic IEnumerable<T>). This interface provides a GetEnumerator method that returns an enumerator capable of traversing the collection.

public class SimpleList : IEnumerable
{
    private int[] _items = {1, 2, 3};
    public IEnumerator GetEnumerator()
    {
        foreach (var item in _items)
            yield return item;
    }
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

Key benefits of implementing IEnumerable:

  • Compatibility with LINQ queries.
  • Support for foreach syntax.
  • Ability to create custom iteration logic.

Quiz Insight: The correct answer "IEnumerable" underscores the central role of this interface in collection iteration.

7. Using Directives and Namespaces

The using directive simplifies code by importing a namespace, allowing you to reference its types without fully qualifying their names. It does not allocate memory or manage resources; those responsibilities belong to the using statement (different from the directive).

using System;
using System.Collections.Generic;

public class Demo
{
    List names = new List(); // No need for System.Collections.Generic.List
}

Best practices:

  • Place using statements at the top of the file.
  • Group related namespaces together for readability.
  • Avoid wildcard imports; be explicit to improve compile‑time performance.

Quiz Insight: The answer "To import a namespace so its types can be referenced without full qualification" captures the purpose of the directive.

8. Structs: Value Types in C#

Structs are lightweight value types that are stored directly on the stack (or inline within other objects). They differ from classes, which are reference types allocated on the heap. Because structs are copied by value, they are ideal for small, immutable data structures such as points, colors, or complex numbers.

public struct Point
{
    public int X { get; set; }
    public int Y { get; set; }
    public Point(int x, int y) : this()
    {
        X = x; Y = y;
    }
}

Important characteristics:

  • Structs cannot inherit from other structs or classes (except System.ValueType).
  • They can implement interfaces.
  • Parameterless constructors are not allowed; you must use the default constructor or define a custom one with parameters.

Quiz Insight: The statement "Structs are value types stored directly on the stack" is the correct description.

9. Summary and Further Learning Paths

By mastering these fundamentals—access modifiers, casting, properties, method overriding, static members, collection interfaces, using directives, and structs—you have built a solid foundation for advanced C# topics such as generics, async programming, and dependency injection.

  • Practice creating classes that combine these concepts.
  • Explore the System.Collections.Generic namespace for type‑safe collections.
  • Delve into LINQ to query IEnumerable data sources efficiently.

Continue your journey by building small projects, reviewing official Microsoft documentation, and participating in coding challenges. The concepts covered here are not only essential for exams but also for real‑world software development.