📖 Scenario: Imagine you want to create a method that can work with any type of data, like numbers, words, or even objects. Instead of writing many methods for each type, you can write one generic method that works for all.
🎯 Goal: You will build a generic method called DisplayItem that takes one input of any type and prints it. This helps you understand how to write methods that are flexible and reusable.
📋 What You'll Learn
Create a generic method named DisplayItem with one type parameter T.
The method should take one parameter of type T named item.
Inside the method, print the item to the console.
Call the DisplayItem method with different types: an integer, a string, and a double.
💡 Why This Matters
🌍 Real World
Generic methods are used in many programs to handle different data types without repeating code. For example, sorting lists of numbers or strings using the same method.
💼 Career
Understanding generic methods is important for writing clean, reusable code in professional C# development, especially in libraries and frameworks.
Progress0 / 4 steps
1
Create a basic C# program structure
Write a C# program with a Program class and a Main method inside it. Leave the Main method empty for now.
C Sharp (C#)
Hint
Start by writing the class and the main method exactly as shown.
2
Declare a generic method named DisplayItem
Inside the Program class but outside the Main method, declare a generic method called DisplayItem with a type parameter T. It should take one parameter named item of type T.
C Sharp (C#)
Hint
Use the syntax static void DisplayItem<T>(T item) to declare the generic method.
3
Print the item inside the generic method
Inside the DisplayItem method, write code to print the item parameter to the console using Console.WriteLine.
C Sharp (C#)
Hint
Use Console.WriteLine(item); to print the value.
4
Call the generic method with different types
Inside the Main method, call DisplayItem three times with these exact values: 42 (int), "Hello" (string), and 3.14 (double).
C Sharp (C#)
Hint
Call the method exactly as DisplayItem(42);, DisplayItem("Hello");, and DisplayItem(3.14);.
Practice
(1/5)
1. What is the purpose of declaring a generic method in C# using <T>?
easy
A. To allow the method to work with any data type specified when called
B. To restrict the method to only work with integers
C. To make the method private
D. To declare a method that returns void only
Solution
Step 1: Understand generic method declaration
Generic methods use <T> to represent a placeholder for any type, making the method flexible.
Step 2: Identify the purpose of <T>
The <T> allows the method to accept or return any type specified at call time, improving reuse.
Final Answer:
To allow the method to work with any data type specified when called -> Option A
Quick Check:
Generic <T> means flexible type [OK]
Hint: Generic <T> means method works with any type [OK]
Common Mistakes:
Thinking <T> restricts to one type like int
Confusing generic methods with access modifiers
Assuming generic methods always return void
2. Which of the following is the correct syntax to declare a generic method named PrintItem that takes one parameter of type T?
easy
A. public void PrintItem<T>(item T) { Console.WriteLine(item); }
B. public <T> void PrintItem(T item) { Console.WriteLine(item); }
C. public void PrintItem<T>(T item) { Console.WriteLine(item); }
D. public void PrintItem(T item) { Console.WriteLine(item); }
Solution
Step 1: Review generic method syntax
The generic type parameter <T> must come immediately after the method name.
Step 2: Check parameter declaration
The parameter should be declared as type T, followed by the parameter name.
Final Answer:
public void PrintItem<T>(T item) { Console.WriteLine(item); } -> Option C
Quick Check:
Generic <T> after method name, parameter type T [OK]
Hint: Put <T> right after method name for generics [OK]
Common Mistakes:
Placing <T> before return type
Omitting <T> after method name
Swapping parameter type and name
3. What will be the output of the following code?
public class Program {
public static void ShowType<T>(T value) {
Console.WriteLine(typeof(T));
}
public static void Main() {
ShowType(123);
ShowType("hello");
}
}
medium
A. 123
hello
B. System.Int32
System.String
C. T
T
D. System.Object
System.Object
Solution
Step 1: Understand typeof(T) in generic method
typeof(T) returns the actual type used when the method is called.
Step 2: Analyze calls ShowType(123) and ShowType("hello")
First call uses int (System.Int32), second uses string (System.String).
Final Answer:
System.Int32
System.String -> Option B
Quick Check:
typeof(T) shows actual type [OK]
Hint: typeof(T) prints actual type used at call [OK]
Common Mistakes:
Expecting value output instead of type
Thinking T is printed literally
Assuming typeof(T) always returns System.Object
4. Identify the error in this generic method declaration:
public void Swap<T>(T a, T b) {
T temp = a;
a = b;
b = temp;
}
medium
A. Cannot declare variable temp of type T
B. Missing generic type declaration <T> after method name
C. Method must return a value to swap
D. Cannot swap values because parameters are passed by value
Solution
Step 1: Check parameter passing
Parameters a and b are passed by value, so swapping inside method won't affect caller.
Step 2: Confirm generic syntax correctness
The method correctly declares <T> after method name and uses T type variables.
Final Answer:
Cannot swap values because parameters are passed by value -> Option D
Quick Check:
Value parameters can't swap caller variables [OK]
Hint: Swap needs ref/out to affect caller variables [OK]
Common Mistakes:
Thinking generic syntax is wrong
Assuming temp can't be type T
Expecting method must return swapped values
5. You want to write a generic method FindMax that returns the maximum of two values of any type T. Which constraint should you add to T to make comparison possible?
hard
A. where T : IComparable<T>
B. where T : class
C. where T : new()
D. where T : struct
Solution
Step 1: Understand comparison requirement
To compare two values of type T, T must support comparison operations.
Step 2: Identify suitable generic constraint
IComparable<T> interface defines CompareTo method for comparing instances.
Step 3: Review other constraints
class restricts to reference types, new() requires parameterless constructor, struct restricts to value types but no comparison guarantee.
Final Answer:
where T : IComparable<T> -> Option A
Quick Check:
Use IComparable<T> to enable comparisons [OK]
Hint: Use IComparable<T> constraint for comparing generics [OK]
Common Mistakes:
Using class or struct constraints instead of IComparable
Forgetting to add any constraint
Assuming new() enables comparison
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.