Getting type information at runtime
📖 Scenario: Imagine you are building a simple program that needs to check what kind of objects it is working with while running. This is like checking the label on a box before opening it to know what's inside.
🎯 Goal: You will create a program that stores different objects, then finds out and prints their types while the program is running.
📋 What You'll Learn
Create a list called
items with different types of objectsCreate a variable called
count to store the number of itemsUse a
for loop with variable i to go through itemsInside the loop, get the type of each item using
GetType()Print the type name of each item using
Console.WriteLine💡 Why This Matters
🌍 Real World
Programs often need to check what kind of data they are working with to decide what to do next. For example, a program that reads files might check if the content is text or numbers.
💼 Career
Understanding how to get type information at runtime helps in debugging, logging, and writing flexible code that can handle different data types safely.
Progress0 / 4 steps