Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Inspecting methods and properties in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Reflection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of MethodInfo Name Property
What will be the output of this C# code snippet that uses reflection to get method names?
C Sharp (C#)
using System;
using System.Reflection;

class Sample {
    public void Hello() { }
    private int Add(int a, int b) { return a + b; }
}

class Program {
    static void Main() {
        MethodInfo[] methods = typeof(Sample).GetMethods(BindingFlags.Public | BindingFlags.Instance);
        foreach (var method in methods) {
            Console.WriteLine(method.Name);
        }
    }
}
A
Hello
Add
B
Add
Equals
GetHashCode
GetType
ToString
C
Hello
Add
Equals
GetHashCode
GetType
ToString
D
Hello
Equals
GetHashCode
GetType
ToString
Attempts:
2 left
πŸ’‘ Hint
Remember that BindingFlags.Public only includes public methods, so private methods like Add won't appear.
❓ Predict Output
intermediate
2:00remaining
PropertyInfo GetValue Output
What will be printed by this C# program that uses reflection to get a property value?
C Sharp (C#)
using System;
using System.Reflection;

class Person {
    public string Name { get; set; } = "Alice";
}

class Program {
    static void Main() {
        Person p = new Person();
        PropertyInfo prop = typeof(Person).GetProperty("Name");
        Console.WriteLine(prop.GetValue(p));
    }
}
AAlice
BName
Cnull
DSystem.String
Attempts:
2 left
πŸ’‘ Hint
GetValue returns the value of the property for the given object instance.
πŸ”§ Debug
advanced
2:00remaining
Why does this reflection code throw an exception?
This code throws a NullReferenceException. What is the cause?
C Sharp (C#)
using System;
using System.Reflection;

class Car {
    private int speed = 100;
}

class Program {
    static void Main() {
        Car c = new Car();
        PropertyInfo prop = typeof(Car).GetProperty("speed");
        Console.WriteLine(prop.GetValue(c));
    }
}
AThe class Car does not have any properties, only fields, so GetProperty returns null.
BThe field 'speed' is not initialized, causing a null value.
CGetValue requires a static object for private properties.
DThe property 'speed' is private and GetProperty without BindingFlags cannot find it, so prop is null.
Attempts:
2 left
πŸ’‘ Hint
Check if 'speed' is a property or a field and how GetProperty works.
πŸ“ Syntax
advanced
2:00remaining
Which code correctly gets all public properties including inherited ones?
Choose the correct C# code snippet that retrieves all public instance properties of a class including those inherited.
Atypeof(MyClass).GetProperties(BindingFlags.NonPublic | BindingFlags.Instance)
Btypeof(MyClass).GetProperties(BindingFlags.Public | BindingFlags.Instance)
Ctypeof(MyClass).GetProperties(BindingFlags.Public | BindingFlags.Static)
Dtypeof(MyClass).GetProperties(BindingFlags.NonPublic | BindingFlags.Static)
Attempts:
2 left
πŸ’‘ Hint
Public instance properties include those with public getters/setters and are instance members.
πŸš€ Application
expert
3:00remaining
Count properties with public getter and private setter
Given this class, how many properties have a public getter and a private setter?
C Sharp (C#)
using System;

class Book {
    public string Title { get; private set; }
    public string Author { get; set; }
    public int Pages { get; private set; }
    public DateTime Published { get; private set; }
    private string InternalCode { get; set; }
}
A1
B2
C3
D4
Attempts:
2 left
πŸ’‘ Hint
Check each property’s getter and setter accessibility carefully.

Practice

(1/5)
1. Which C# method is used to get the type information of an object instance?
easy
A. typeof()
B. GetMethods()
C. GetProperties()
D. GetType()

Solution

  1. Step 1: Understand the difference between GetType() and typeof()

    GetType() is called on an object instance to get its runtime type, while typeof() is used with a type name at compile time.
  2. Step 2: Identify the method that works on an object instance

    Since the question asks for the method used on an object instance, GetType() is the correct choice.
  3. Final Answer:

    GetType() -> Option D
  4. Quick Check:

    Object instance method for type info = GetType() [OK]
Hint: Use GetType() on objects, typeof() on type names [OK]
Common Mistakes:
  • Confusing typeof() with GetType()
  • Using GetMethods() to get type info
  • Thinking GetProperties() returns type
2. Which of the following is the correct syntax to get all methods of the String type in C#?
easy
A. typeof(String).GetMethods()
B. typeof(String).GetProperties()
C. String.GetMethods()
D. GetType(String).GetMethods()

Solution

  1. Step 1: Recall how to get methods from a type

    Use typeof(TypeName).GetMethods() to get all methods of a type.
  2. Step 2: Check each option's syntax

    typeof(String).GetMethods() uses typeof(String).GetMethods(), which is correct. typeof(String).GetProperties() calls GetProperties(), not methods. String.GetMethods() tries to call GetMethods() on the type name directly, which is invalid. GetType(String).GetMethods() uses GetType(String), which is incorrect syntax.
  3. Final Answer:

    typeof(String).GetMethods() -> Option A
  4. Quick Check:

    Get methods from type = typeof(Type).GetMethods() [OK]
Hint: Use typeof(Type).GetMethods() to list methods [OK]
Common Mistakes:
  • Calling GetMethods() on type name directly
  • Mixing GetProperties() with GetMethods()
  • Using GetType() with a type name instead of instance
3. What will the following C# code output?
var props = typeof(DateTime).GetProperties();
Console.WriteLine(props.Length);
medium
A. 0
B. More than 0
C. Compilation error
D. Runtime exception

Solution

  1. Step 1: Understand what GetProperties() returns

    GetProperties() returns an array of all public properties of the type.
  2. Step 2: Check if DateTime has properties

    DateTime has many properties like Now, Today, Year, Month, etc., so the array length will be more than zero.
  3. Final Answer:

    More than 0 -> Option B
  4. Quick Check:

    DateTime properties count > 0 [OK]
Hint: Types like DateTime have many properties, so length > 0 [OK]
Common Mistakes:
  • Assuming no properties means length 0
  • Expecting compilation or runtime errors
  • Confusing methods with properties
4. Identify the error in this C# code snippet:
var typeInfo = "hello".GetType();
var methods = typeInfo.GetMethod();
medium
A. GetType() is invalid on string
B. Missing parentheses on GetType
C. GetMethod() should be GetMethods()
D. Cannot call GetMethod() on typeInfo

Solution

  1. Step 1: Check usage of GetType()

    "hello".GetType() is valid and returns the type of the string instance.
  2. Step 2: Check method name on typeInfo

    There is no method called GetMethod() without parameters; to get all methods, use GetMethods().
  3. Final Answer:

    GetMethod() should be GetMethods() -> Option C
  4. Quick Check:

    Use GetMethods() to get all methods [OK]
Hint: Use GetMethods() (plural) to list all methods [OK]
Common Mistakes:
  • Using GetMethod() without parameters
  • Thinking GetType() is invalid on string
  • Missing parentheses on method calls
5. You want to list all public properties of a custom class Person and print their names. Which code snippet correctly does this?
hard
A. var props = typeof(Person).GetProperties(); foreach(var p in props) Console.WriteLine(p.Name);
B. var props = new Person().GetProperties(); foreach(var p in props) Console.WriteLine(p.Name);
C. var props = typeof(Person).GetMethods(); foreach(var p in props) Console.WriteLine(p.Name);
D. var props = Person.GetProperties(); foreach(var p in props) Console.WriteLine(p.Name);

Solution

  1. Step 1: Identify how to get properties of a type

    Use typeof(Person).GetProperties() to get all public properties of the Person class.
  2. Step 2: Check the foreach loop to print property names

    Iterate over the properties and print each p.Name to display property names.
  3. Step 3: Verify other options

    var props = new Person().GetProperties(); foreach(var p in props) Console.WriteLine(p.Name); tries to call GetProperties() on an instance, which is invalid. var props = typeof(Person).GetMethods(); foreach(var p in props) Console.WriteLine(p.Name); gets methods, not properties. var props = Person.GetProperties(); foreach(var p in props) Console.WriteLine(p.Name); calls GetProperties() directly on the class name, which is invalid syntax.
  4. Final Answer:

    var props = typeof(Person).GetProperties(); foreach(var p in props) Console.WriteLine(p.Name); -> Option A
  5. Quick Check:

    Use typeof(Type).GetProperties() and loop to print names [OK]
Hint: Use typeof(Class).GetProperties() and loop to print names [OK]
Common Mistakes:
  • Calling GetProperties() on an instance
  • Using GetMethods() instead of GetProperties()
  • Calling GetProperties() directly on class name
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard