Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# Generic constraint to superclass?

TheUninvited

Active Coder
So kinda learning about generics in c#

and i was asking
where do we use Generics?and some ideas on where to use them ?

and this guy said :
One important thing about Generics: You are able to constrain them to another superclass for example. This gives you then the possibility to execute code of the superclass in a generic context.

What does it mean? do you guys maybe know?
 
Advantages of Generics:
  • Re-usability: You can use a single generic type definition for multiple purposes in the same code without any method overriding or code modification. For example, you can create a generic method to perform a math operation. You could then use the method to perform the same operation on double float, or int.
  • Type Safety: Generic data types provide better type safety, especially in the case of collections, which is where you will most likely be using them the most from the System.Collections.Generics namespace.
  • Performance: Generic types provide better performance because they reduce the need for boxing, unboxing, just read this stackoverflow post for more information about this subject.
As you are learning generics here is some code you can use them in methods and getters as well this is from a RuneScape private server project of mine that is written in C#:

C#:
   public class CoroutinesManager
    {
        private readonly IDictionary<string, Tuple<CoroutineThread, ConquerCoroutine>> _coroutines;

        public CoroutinesManager()
        {
            Conquer.Log(GetType().Name, "Started building coroutines...");
            _coroutines = new Dictionary<string, Tuple<CoroutineThread, ConquerCoroutine>>
            {
                {
                    "login_queue", new Tuple<CoroutineThread, ConquerCoroutine>(
                        new CoroutineThread(), new LoginQueueCoroutine())
                }
            };
        }

        public void Load()
        {
            var tuples = _coroutines.Values.Select(tuple => tuple);

            foreach (var (executor, coroutine) in tuples)
            {
                executor.Start();
                executor.AddCoroutine(coroutine);
            }
            Conquer.Log(GetType().Name, $"{_coroutines.Count} Coroutines were loaded.");
        }

        public void AddCoroutineExecutor<T>(string safeName, int threadMaxMsCycle, params object[] parameters) where T : ConquerCoroutine, new()
        {
            _coroutines.Add(safeName, new Tuple<CoroutineThread, ConquerCoroutine>(
                new CoroutineThread(), parameters != null ? (T) Activator.CreateInstance(
                    typeof(T), parameters) : new T()));
        }

        public T Get<T>() where T : ConquerCoroutine
        {
            var coroutines = _coroutines.Select(c => c.Value).Select(t => t.Item2);
            return coroutines.Where(coroutine => coroutine.GetType() == typeof(T)).Cast<T>().First();
        }

        public CoroutineThread GetExecutor(string safeName) =>  _coroutines[safeName].Item1;
      
    }
 
Last edited by a moderator:
Advantages of Generics:
  • Re-usability: You can use a single generic type definition for multiple purposes in the same code without any method overriding or code modification. For example, you can create a generic method to perform a math operation. You could then use the method to perform the same operation on double float, or int.
  • Type Safety: Generic data types provide better type safety, especially in the case of collections, which is where you will most likely be using them the most from the System.Collections.Generics namespace.
  • Performance: Generic types provide better performance because they reduce the need for boxing, unboxing, just read this stackoverflow post for more information about this subject.
As you are learning generics here is some code you can use them in methods and getters as well this is from a RuneScape private server project of mine that is written in C#:

C#:
   public class CoroutinesManager
    {
        private readonly IDictionary<string, Tuple<CoroutineThread, ConquerCoroutine>> _coroutines;

        public CoroutinesManager()
        {
            Conquer.Log(GetType().Name, "Started building coroutines...");
            _coroutines = new Dictionary<string, Tuple<CoroutineThread, ConquerCoroutine>>
            {
                {
                    "login_queue", new Tuple<CoroutineThread, ConquerCoroutine>(
                        new CoroutineThread(), new LoginQueueCoroutine())
                }
            };
        }

        public void Load()
        {
            var tuples = _coroutines.Values.Select(tuple => tuple);

            foreach (var (executor, coroutine) in tuples)
            {
                executor.Start();
                executor.AddCoroutine(coroutine);
            }
            Conquer.Log(GetType().Name, $"{_coroutines.Count} Coroutines were loaded.");
        }

        public void AddCoroutineExecutor<T>(string safeName, int threadMaxMsCycle, params object[] parameters) where T : ConquerCoroutine, new()
        {
            _coroutines.Add(safeName, new Tuple<CoroutineThread, ConquerCoroutine>(
                new CoroutineThread(), parameters != null ? (T) Activator.CreateInstance(
                    typeof(T), parameters) : new T()));
        }

        public T Get<T>() where T : ConquerCoroutine
        {
            var coroutines = _coroutines.Select(c => c.Value).Select(t => t.Item2);
            return coroutines.Where(coroutine => coroutine.GetType() == typeof(T)).Cast<T>().First();
        }

        public CoroutineThread GetExecutor(string safeName) =>  _coroutines[safeName].Item1;
     
    }
you are awesome you know that right?

thank you so much dude
really
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom