0
0
C Sharp (C#)programming~20 mins

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

Choose your learning style9 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.