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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:40:19+00:00 2026-06-13T15:40:19+00:00

How can I put the type of a variable in a method parameter as

  • 0

How can I put the type of a variable in a method parameter as something is defined by a class variable? For example:

class MyClass {
   private Type _type;

   public MyClass(Type type) {
      _type = type;
   }

   public void SomeMethod(_type param) { //... }
   public _type OtherMethod() {}

}

So, the idea is, I could set a dynamic type to a variable in the class, and use the Type variable as the type for other objects.

Is it possible to do this in C#?

Edit:

I decided to make my question clearer and explain on why I am asking for such a feature. I have tried Generics. The problem with generics, however, is that I have to declare the type for the class every time I refer to an object of that class like: MyClass<TSomeType> param

In my scenario, I have a List<MyClass> dataList that contains MyClass. If I had a generic on MyClass, then dataList has to be List<MyClass<TSomeType>>. In this case, I am stuck because the list can only consist of MyClass<TSomeType>. I cannot have other kinds of Types once I declared the type for the whole class. This is the reason why I want to know if there is a more dyanmic way of declaring a Type, like I could store the type of a class to a variable, and then use this variable like a class type.

  • 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-13T15:40:21+00:00Added an answer on June 13, 2026 at 3:40 pm

    Is it possible to do this in C#?

    No. What would the compiler do with a MyClass? It couldn’t possibly know whether a method call was valid or not.

    You can use generics instead though:

    class MyClass<T>
    {
        public void SomeMethod(T param) { ... }
        public T OtherMethod() { ... }
    }
    

    At this point, when the compiler sees a MyClass<string> it knows that SomeMethod("foo") is valid, but SomeMethod(10) isn’t.

    If you really won’t know the type until execution time, then you might as well just use object:

    class MyClass
    {
        public void SomeMethod(object param) { ... }
        public object OtherMethod() { ... }
    }
    

    … and potentially do execution-time checking against a Type if you really want to.

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

Sidebar

Related Questions

In C++ you can put a member in a base class and a member
I have a class with a delegate declaration as follows... Public Class MyClass Public
How can you get the data type of a variable argument in Java if
In C#, an anonymous type can be as follows: method doStuff(){ var myVar =
I can put python doctests in the bodies of each function, which I sometimes
We can put our file say one.txt in txt folder available in tomcat->webapp folder.
In XAML we can put one long string in the positions property, How do
how i can put multiple buttons in scroll view with according to my requirement?
I know I can put escaped HTML tags in string resources. However, looking at
Where are all of the places you can put CSS style information on an

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.