Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Implementing a Generic Class | Generics & Reflection
C# Desktop Development with .NET MAUI

Challenge: Implementing a Generic Class

Pyyhkäise näyttääksesi valikon

You can clone the base code from the GithHub Repository.

A stack is a special kind of data structure which stores data according to the Last In, First Out (LIFO) principle. A stack has two main methods, namely, Push and Pop. The Push adds a new element to the stack, and Pop removes the most recently added element from the stack. You can imagine it like a stack of books, or any other item. By using Push you add a new item on the top, and by using Pop you remove the item at the top. This is the LIFO principle.

The program contains a class which implements the Stack data structure, however, it only supports integers. Make changes in the class code so that it is able to support any data type.

Hint
expand arrow

The syntax for creating a generic class is class className<T1, T2, …> { /* class code here */ }.

Solution
expand arrow
namespace ConsoleApp
{
    internal class Program
    {
        public class Stack&lt;T&gt;
        {
            private List&lt;T&gt; items;

            public Stack()
            {
                items = new List&lt;T&gt;();
            }

            public void Push(T item)
            {
                items.Add(item);
            }

            public T Pop()
            {
                if (items.Count == 0)
                    throw new InvalidOperationException("Stack is empty");

                T poppedItem = items[items.Count - 1];
                items.RemoveAt(items.Count - 1);
                return poppedItem;
            }

            // Gets the last element, without removing it
            public T Peek()
            {
                if (items.Count == 0)
                {
                    throw new InvalidOperationException("Stack is empty");
                }

                return items[items.Count - 1];
            }

            public int Count
            {
                get { return items.Count; }
            }

            public bool IsEmpty
            {
                get { return items.Count == 0; }
            }
        }

        static void Main(string[] args)
        {
            Stack&lt;string&gt; s1 = new Stack&lt;string&gt;();
            s1.Push("a");
            s1.Push("b");
            s1.Push("c");

            Console.WriteLine(s1.Pop());
            Console.WriteLine(s1.Pop());
            Console.WriteLine(s1.Pop());
        }
    }
}
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 5

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Challenge: Implementing a Generic Class

You can clone the base code from the GithHub Repository.

A stack is a special kind of data structure which stores data according to the Last In, First Out (LIFO) principle. A stack has two main methods, namely, Push and Pop. The Push adds a new element to the stack, and Pop removes the most recently added element from the stack. You can imagine it like a stack of books, or any other item. By using Push you add a new item on the top, and by using Pop you remove the item at the top. This is the LIFO principle.

The program contains a class which implements the Stack data structure, however, it only supports integers. Make changes in the class code so that it is able to support any data type.

Hint
expand arrow

The syntax for creating a generic class is class className<T1, T2, …> { /* class code here */ }.

Solution
expand arrow
namespace ConsoleApp
{
    internal class Program
    {
        public class Stack&lt;T&gt;
        {
            private List&lt;T&gt; items;

            public Stack()
            {
                items = new List&lt;T&gt;();
            }

            public void Push(T item)
            {
                items.Add(item);
            }

            public T Pop()
            {
                if (items.Count == 0)
                    throw new InvalidOperationException("Stack is empty");

                T poppedItem = items[items.Count - 1];
                items.RemoveAt(items.Count - 1);
                return poppedItem;
            }

            // Gets the last element, without removing it
            public T Peek()
            {
                if (items.Count == 0)
                {
                    throw new InvalidOperationException("Stack is empty");
                }

                return items[items.Count - 1];
            }

            public int Count
            {
                get { return items.Count; }
            }

            public bool IsEmpty
            {
                get { return items.Count == 0; }
            }
        }

        static void Main(string[] args)
        {
            Stack&lt;string&gt; s1 = new Stack&lt;string&gt;();
            s1.Push("a");
            s1.Push("b");
            s1.Push("c");

            Console.WriteLine(s1.Pop());
            Console.WriteLine(s1.Pop());
            Console.WriteLine(s1.Pop());
        }
    }
}
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 5
some-alt