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 3499996
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:40:47+00:00 2026-05-18T12:40:47+00:00

I am going through a book trying to understand Generics with C# and I

  • 0

I am going through a book trying to understand Generics with C# and I have come across an example I don’t understand. Here is the sample code.

using System;

public class Printer
{
  public void Print<T>(T argument)
  {
    Console.WriteLine(argument.ToString());
  }

  static void Main(string[] arguments)
  {
    Printer printer = new Printer();
    printer.Print<string>("Hello, World");
    Console.WriteLine("Done");
    Console.ReadKey();
  }
}

What is confusing me is the argument to the Print method. I understand using a generic type placeholder when dealing with a collections such as List<T>. However what I don’t understand is how <T> comes into play with a method? Is the code just saying that the type of the parameter passed into the Print() method is just not known at design time and could be anything? Could someone help me decipher this? Thank you.

  • 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. Editorial Team
    Editorial Team
    2026-05-18T12:40:47+00:00Added an answer on May 18, 2026 at 12:40 pm

    By declaring your method with a generic type, you make your method more flexible as it can then work with variables of any type you choose, including primitive types (unless you specify where T : class of course).

    Another very common example that much better illustrates one use of a generic method is a Swap<T>(T, T) method:

    /* 
     * The ref keywords mean "pass by reference" i.e. modify the variables as they
     * were passed into the method.
     *
     * The <T> in the signature tells the compiler that T is a generic type, in case
     * the class itself doesn't already declare T a generic type.
     */
    public void Swap<T>(ref T x, ref T y)
    {
        // Tells the compiler that temp should be of the same type as x and y,
        // regardless of what type T may be
        T temp = x;
        x = y;
        y = temp;
    }
    
    int x = 3, y = 6;
    Swap<int>(ref x, ref y);
    Console.WriteLine(x + " " + y);
    
    char a = 'a', b = 'b';
    Swap<char>(ref a, ref b);
    Console.WriteLine(a + " " + b);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been going through the book Learning WCF trying to get my head
I was going through some code and came across a scenario where my combobox
I'm going through some old stored procedures at work and constantly come across CASE
Please help me with this code - I am basically going through a book
I've been going through a beginning Python book, and I've been trying to write
$info['schedule'] = array('swimming', 'soccer', 'read book'); Just going through a tutorial and came across
I was going through the AWDR book on web development with ruby on rails
After going through the Appendix A, C# Coding Style Conventions of the great book
We're going through a massive migration project at the minute and trying to validate
I'm going through SICP and I'd like to have an interpreter analogous to the

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.