- 2

Generic Classes

The class Elems creates a 3-sized array of integers, defines Add() and Show() methods to store the elements into the array, and shows them separated by a space. Modify the class to make it generic to execute the same actions with string type, given in the Main function. You need to replace the int type by the generic <T> type. Each output should end with a space (including the last one).

18th Jan 2023, 2:21 AM
Angel Gabriel Partida Garduño
5 Antworten
+ 2
AND your code Angel Gabriel is ??? WE are not a code for you platform. Please show us your attempt so we can help you, not do your learning for you. THANKS
18th Jan 2023, 2:28 AM
BroFar
BroFar - avatar
+ 2
using System; using System.Collections.Generic; namespace SoloLearn { class Program { static void Main(string[] args) { Elems<string> elems1 = new Elems<string>(); elems1.Add("John", "Tamara", "David"); elems1.Show(); Console.WriteLine(); Elems<int> elems2 = new Elems<int>(); elems2.Add(5, 14, 13); elems2.Show(); } } //make this class generic class Elems<T> { public T[] elements = new T[3]; public void Add(T a,T b, T c) { elements[0]=a; elements[1]=b; elements[2]=c; } public void Show() { foreach (T item in elements) { Console.Write(item + " "); } } } }
17th Oct 2023, 6:41 AM
yue w
0
using System; using System.Collections.Generic; namespace SoloLearn { class Program { static void Main(string[] args) { Elems<string> elems1 = new Elems<string>(); elems1.Add("John", "Tamara", "David"); elems1.Show(); Console.WriteLine(); Elems<int> elems2 = new Elems<int>(); elems2.Add(5, 14, 13); elems2.Show(); } } //make this class generic class Elems { public int[] elements = new int[3]; public void Add(int elem1, int elem2, int elem3) { elements[0] = elem1; elements[1] = elem2; elements[2] = elem3; } public void Show() { foreach (int item in elements) { Console.Write(item + " "); } } } }
18th Jan 2023, 3:00 AM
Angel Gabriel Partida Garduño
0
what the copy paste!!! Use link on your code and put it here..
18th Jan 2023, 5:56 AM
Smith Welder
Smith Welder - avatar
0
"Modify the class to make it generic to execute the same actions with string type, given in the Main function. You need to replace the int type by the generic <T> type." Your class Elems now takes only int. Make it as generics.. Instructions are already clear in description..
18th Jan 2023, 8:35 AM
Jayakrishna 🇮🇳