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

Inspecting methods and properties in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Inspecting methods and properties
Create object instance
↓
Access type info
↓
Get methods list
↓
Get properties list
↓
Display names and details
↓
End
This flow shows how to create an object, get its type info, then list and display its methods and properties.
Execution Sample
C Sharp (C#)
using System;
using System.Reflection;

var obj = "Hello";
var type = obj.GetType();
var methods = type.GetMethods();
var properties = type.GetProperties();
This code creates a string object, gets its type, then retrieves its methods and properties.
Execution Table
StepActionEvaluationResult
1Create object instanceobj = "Hello"obj is a string object with value "Hello"
2Get type infotype = obj.GetType()type is System.String type info
3Get methods listmethods = type.GetMethods()methods is array of MethodInfo objects for System.String
4Get properties listproperties = type.GetProperties()properties is array of PropertyInfo objects for System.String
5Display method namesforeach method in methodsPrint method.Name for each method
6Display property namesforeach property in propertiesPrint property.Name for each property
7EndNo more actionsProgram ends
💡 All methods and properties of the object's type have been inspected and displayed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
objnull"Hello""Hello""Hello""Hello""Hello"
typenullnullSystem.StringSystem.StringSystem.StringSystem.String
methodsnullnullnullMethodInfo[]MethodInfo[]MethodInfo[]
propertiesnullnullnullnullPropertyInfo[]PropertyInfo[]
Key Moments - 3 Insights
Why do we call GetType() on the object before getting methods or properties?
GetType() returns the type information of the object, which is needed to access its methods and properties via reflection, as shown in step 2 of the execution_table.
Are methods and properties the same thing when inspecting a type?
No, methods are actions the object can perform, while properties are data values it holds. They are retrieved separately using GetMethods() and GetProperties(), as seen in steps 3 and 4.
Why do we get arrays of MethodInfo and PropertyInfo instead of the actual methods or properties?
Reflection returns metadata objects (MethodInfo, PropertyInfo) describing methods and properties, not the actual executable code or data. This allows inspection without invoking them, as shown in steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'type' after step 2?
Anull
B"Hello"
CSystem.String
DMethodInfo[]
💡 Hint
Check the 'type' variable value in variable_tracker after step 2.
At which step do we get the list of properties of the object?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column in execution_table for getting properties.
If we changed obj to an integer instead of a string, what would change in the execution?
AThe type variable would be System.Int32 instead of System.String
BThe methods array would be empty
CThe properties array would be null
DThe program would crash
💡 Hint
Consider what GetType() returns for different object types as shown in step 2.
Concept Snapshot
Inspecting methods and properties:
- Use obj.GetType() to get type info
- Use type.GetMethods() to get methods list
- Use type.GetProperties() to get properties list
- Methods and properties are metadata objects
- Useful for reflection and dynamic inspection
Full Transcript
This example shows how to inspect an object's methods and properties in C#. First, we create an object instance, here a string "Hello". Then we get its type information using GetType(). Next, we retrieve the list of methods with GetMethods() and the list of properties with GetProperties(). These return arrays of MethodInfo and PropertyInfo objects, which describe the methods and properties without executing them. We can then display their names or other details. This process is called reflection and helps us understand what an object can do or what data it holds at runtime.

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