repl.it
@kironroy/

generics2

C#

No description

fork
loading
Files
  • main.cs
  • main.exe
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;

namespace CoreyGenericsHW
{
    class Program
    {
        static void Main(string[] args)
        {
          Console.WriteLine(); // breakline

            string result = GernericExample(1);
            Console.WriteLine($"Generic example method returns: {result}");

            result = GernericExample(6.4);
            Console.WriteLine($"Generic example method returns: {result}");

            result = GernericExample("Hello");
            Console.WriteLine($"Generic example method returns: {result}");

            result = GernericExample(true);
            Console.WriteLine($"Generic example method returns: {result}");

            Console.ReadLine();
        }
        
        private static string GernericExample<T>(T item)
        {
            var itemAnyType = item.ToString();

            return itemAnyType;
        }

        
    }
}

   // *** GENERICS WRITE CODE ONCE AND USE IT EVERYWHERE ***
            
            // string[] testArray;
            // multiple types can be held in an array

            // boxing and unboxing is expenisve when there are differnet types
            // List array allows developers to strongly type something and 
            // use same def for different types
            // example below