Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 171099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:55:38+00:00 2026-05-11T12:55:38+00:00

(Thanks everyone for the answers, here is my refactored example , in turn another

  • 0

(Thanks everyone for the answers, here is my refactored example, in turn another StackOverflow question about the Single Responsibility Principle.)

Coming from PHP to C#, this syntax was intimidating:

container.RegisterType<Customer>('customer1'); 

until I realized it expresses the same thing as:

container.RegisterType(typeof(Customer), 'customer1'); 

as I demonstrate in the code below.

So is there some reason why generics is used here (e.g. throughout Unity and most C# IoC containers) other than it just being a cleaner syntax, i.e. you don’t need the typeof() when sending the type?

using System;  namespace TestGenericParameter {     class Program     {         static void Main(string[] args)         {             Container container = new Container();             container.RegisterType<Customer>('test');             container.RegisterType(typeof(Customer), 'test');              Console.ReadLine();         }     }      public class Container     {         public void RegisterType<T>(string dummy)         {             Console.WriteLine('Type={0}, dummy={1}, name of class={2}', typeof(T), dummy, typeof(T).Name);         }          public void RegisterType(Type T, string dummy)         {             Console.WriteLine('Type={0}, dummy={1}, name of class={2}', T, dummy, T.Name);         }      }      public class Customer {} }  //OUTPUT: //Type=TestGenericParameter.Customer, dummy=test, name of class=Customer //Type=TestGenericParameter.Customer, dummy=test, name of class=Customer 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:55:39+00:00Added an answer on May 11, 2026 at 12:55 pm

    A primary reason is the type safety at compile time. If you are passing two Type objects you are placing the responsibility at the developer instead of the compiler.

    This is also why many IoC containers utilizes it, as your compiler will complain if an concrete type isn’t inheriting the abstract type.

    public void Register<TAbstract, TConcrete>() where TConcrete : TAbstract { } 

    This code will only work if TConcrete is implementing or inheriting TAbstract. If this method took two Type parameters, your method should validate this relationship.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello and thanks to everyone for reading my question. I've been working on a
This is my first question. Thanks to everyone who contributes to this site, it's
I have read lots of questions and answers about this issue on StackOverflow, but
Thanks for those wonderful answers everyone.I tried everything said by @Jan, @Ratna yet I
Thanks everybody. (For some reason I couldn't up vote answers, so everyone wins today
Thanks everyone for taking the time to read this. I have styled my navigation
update: I mistyped 2 variables...so embarrassing. thanks everyone for the effort! sorry i find
Thanks to everyone out there helping newbies like me. So far I have this:
I was reading a question posted here: Sudoku algorithm in C# And one of
--Solved by Elliot B. Thanks! May also take into account the other modifications. Here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.