-Annie-

GenericCountMethodString

Jul 17th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. namespace GenericCountMethodString
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.  
  6.     public class GenericCountMethodString
  7.     {
  8.         public static void Main()
  9.         {
  10.             List<Box<string>> boxes = new List<Box<string>>();
  11.  
  12.             int boxCount = Int32.Parse(Console.ReadLine());
  13.  
  14.             for (int i = 0; i < boxCount; i++)
  15.             {
  16.                 boxes.Add(new Box<string>(Console.ReadLine()));
  17.             }
  18.  
  19.             Box<string> comparableBox = new Box<string>(Console.ReadLine());
  20.  
  21.             int countOfGreaterelements = CompareElements(boxes, comparableBox);
  22.             Console.WriteLine(countOfGreaterelements);
  23.         }
  24.  
  25.         static int CompareElements<T>(List<Box<T>> boxes, Box<T> comparableBox)
  26.             where T : IComparable<T>
  27.         {
  28.             int count = 0;
  29.  
  30.             foreach (var box in boxes)
  31.             {
  32.                 if (box.Value.CompareTo(comparableBox.Value) > 0)
  33.                 {
  34.                     count++;
  35.                 }
  36.             }
  37.  
  38.             return count;
  39.         }
  40.     }
  41.  
  42.     public class Box<T> where T : IComparable<T>
  43.     {
  44.         private T value;
  45.  
  46.         public Box()
  47.             : this(default(T))
  48.         {
  49.         }
  50.  
  51.         public Box(T value)
  52.         {
  53.             this.Value = value;
  54.         }
  55.  
  56.         public T Value
  57.         {
  58.             get
  59.             {
  60.                 return value;
  61.             }
  62.  
  63.             set
  64.             {
  65.                 this.value = value;
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment