Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- public class Program
- {
- public static void Main()
- {
- List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
- List<string> words = new List<string> { "apple", "banana", "cherry" };
- Console.WriteLine("Count of numbers: " + GetListCount(numbers));
- Console.WriteLine("Count of words: " + GetListCount(words));
- }
- public static int GetListCount<T>(List<T> list)
- {
- // Check if the list type is either int or string, else throw exception
- if (typeof(T) != typeof(int) && typeof(T) != typeof(string))
- {
- throw new ArgumentException("List must be of type int or string.");
- }
- return list.Count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement