Advertisement
ertu2070

Untitled

May 1st, 2024
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Program
  5. {
  6.     public static void Main()
  7.     {
  8.         List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
  9.         List<string> words = new List<string> { "apple", "banana", "cherry" };
  10.  
  11.         Console.WriteLine("Count of numbers: " + GetListCount(numbers));
  12.         Console.WriteLine("Count of words: " + GetListCount(words));
  13.     }
  14.  
  15.     public static int GetListCount<T>(List<T> list)
  16.     {
  17.         // Check if the list type is either int or string, else throw exception
  18.         if (typeof(T) != typeof(int) && typeof(T) != typeof(string))
  19.         {
  20.             throw new ArgumentException("List must be of type int or string.");
  21.         }
  22.  
  23.         return list.Count;
  24.     }
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement