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

  • SEARCH
  • Home
  • 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 8667853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:04:24+00:00 2026-06-12T18:04:24+00:00

I have a generic function static Log<T>(T Log) . I would like to check

  • 0

I have a generic function static Log<T>(T Log). I would like to check type of T and decide what to do next.

This is what I got so far:

public static void Log<T>(T log)
{
    switch (typeof(log))
    { ... }
}

What am I doing wrong? My error is that typeof(log) doesnt work.

  • 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-06-12T18:04:25+00:00Added an answer on June 12, 2026 at 6:04 pm
    switch (expression)
    {
    
    }
    

    expression = An integral or string type expression (From MSDN)

    http://msdn.microsoft.com/en-us/library/06tc147t(v=vs.71).aspx

    The typeof keyword returns neither of these. You can’t use log.GetType() as you need to meet the criteria above of which Type isn’t suitable.

    To be on the safe side I would limit this to if statements with the corresponding types as this will achieve the same thing.

    static void a<T>(T b)
    {
      if (typeof(T) == typeof(B))
        Console.WriteLine("T is B");
      else if(typeof(T) == typeof(C))
        Console.WriteLine("T is C");
    }
    

    EDIT:

    If you have for arguments sake you have:

      public class Vehicle 
      {
        public virtual int MoveVehicle() 
        {
           //Do Logic
           return 0;
        }
      }
      public class Car : Vehicle { }
    

    And you want a generic method to move the Vehicle you could do something called generic type constraints

    static void a<T>(T b) where T : Vehicle
    {
        int newPosition = b.MoveVehicle();
    }
    

    http://msdn.microsoft.com/en-us/library/bb384067.aspx

    T has to be a Vehicle now, so you have access to the methods in Vehicle. You can pass a car to the method and it will still be able to call the MoveVehicle method.

      a<Vehicle>(new Car());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a generic function that have the function definition as: public static
I have a function that accepts a generic parameter T that is of type
Is the Java compiler able to infer the type of a generic static function
I have a generic Logger class that looks like this: class Logger { ...
I have some function like private static UserInfo FillUserInfoFromDataRow(DataRow dr) { UserInfo user =
I have a function with generic return type. If value parameter is a valid
I have a base class ManagerBase that has a generic static function GetManager. There
I want a generic function that would work with types that have Top ,
I would like some feedback on how we can best write a generic function
I have some code for strongly typing Includes()'s in linq, like so... public static

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.