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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:33:36+00:00 2026-05-16T01:33:36+00:00

I have a generic class, class ComputeScalar<T> : IComputeVariable where T : struct {

  • 0

I have a generic class,

class ComputeScalar<T> : IComputeVariable where T : struct
{
   // This is why i have to use generics.
   ComputeBuffer<T> buffer;

   T data;
}

class ComputeArray<T> : IComputeVariable where T : struct
{
   // This is why i have to use generics.
   ComputeBuffer<T> buffer;

   T[] data;
}

and i use this class in a list in another class,

class SomeClass
{
   List<IComputeVariable> variables;
}

I created the interface because in C# we can’t use generic classes for type parameters. (Right?) What i want to learn is how can i make “data” a member of interface? And during runtime how can i determine type of data?
(Data can be any ValueType)

  • 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-16T01:33:37+00:00Added an answer on May 16, 2026 at 1:33 am

    You could only make data a member of the interface by making it weakly typed as object:

    public interface IComputeVariable
    {
        object Data { get; }
    }
    

    (Note that it has to be a property – you can’t specify fields in interfaces.)

    You’d then probably want to implement that explicitly in ComputeScalar<T>, to avoid the weakly typed version from being used where the strongly typed version was available.

    An alternative would be to make the interface generic, and SomeClass too:

    class SomeClass<T> where T : struct
    {
       List<IComputeVariable<T>> variables;
    }
    

    We don’t really know enough about your situation to know which is the right approach, but those are two of your options.

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

Sidebar

Related Questions

I have this generic class public class BinTree<T> { T value; List<BinTree<? extends T>>
I am Currently learning Generics I have this Generic class here package generics.lesson; public
I have a generic class that takes a type T . Within this class
I know Java's generics are somewhat inferior to .Net's. I have a generic class
I have a generic class, says : MyClass<T> Inside a method of this class,
If I have a generic class like this: public class Repository<T> { public string
This is a syntax question. I have a generic class which is inheriting from
I have a generic class in C#, like this: public class GenericClass<T> { ...
I have a generic class like this: class MyGenericClass<T> { } On the other
I have such generic class of list: using System; using System.Collections; using System.Collections.Generic; using

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.