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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:50:47+00:00 2026-05-26T03:50:47+00:00

I have a class: class abc <T> { private T foo; public string a

  • 0

I have a class:

class abc <T> {
  private T foo; 
  public string a {
    set {
       foo = T.parse(value);
    }

    get{
       return foo.toString();
    }

  }
}

However the T.parse command is giving me an error. Anyone of a way to do what I am trying to do?

I am using this as a base class for some other derived classes.

Edit:

What I ended us doing:

Delegate parse = Delegate.CreateDelegate(typeof(Func<String, T>), typeof(T).GetMethod("Parse", new[] { typeof(string) }));

I do that once in the constructor

and then I do the following in my property:

        lock (lockVariable)
        {
            m_result = (T)parse.DynamicInvoke(value);
            dirty = true;
        } 
  • 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-26T03:50:48+00:00Added an answer on May 26, 2026 at 3:50 am

    C# generic types are not C++ templates. A template lets you do a fancy “search and replace” where you would substitute the name of a type that implements a static parse method for T. C# generics are not a textual search-and-replace mechanism like that. Rather, they describe parameterized polymorphism on types. With a template, all that is required is that the specific arguments you substitute for the parameters are all good. With a generic every possible substitution whether you actually do it or not, has got to be good.

    UPDATE:

    A commenter asks:

    What would be the C# way of doing things when an equivalent to Haskell’s Read type class is needed?

    Now we come to the deep question underlying the original question.

    To clarify for the reader unfamiliar with Haskell: Since C# 2.0, C# has supported “generic” types, which are a “higher” kind of type than regular types. You can say List<int> and a new type is made for you that follows the List<T> pattern, but it is a list specifically of integers.

    Haskell supports an even higher kind of type in its type system. With generic types you can say “every MyCollection<T> has a method GetValue that takes an int and returns a T, for any T you care to name”. With generic types you can put constraints on T and say “and furthermore, T is guaranteed to implement IComparable<T>…” With Haskell typeclasses you can go even further and say the moral equivalent of “…and moreover, T is guaranteed to have a static method Parse that takes a string and returns a T”.

    The “Read” typeclass is specifically that typeclass that declares the moral equivalent of “a class C that obeys the Read typeclass pattern is one that has a method Parse that takes a string and returns a C”.

    C# does not support that kind of higher type. If it did then we could typecheck patterns in the language itself such as monads, which today can only be typechecked by baking them into the compiler (in the form of query comprehensions, for example.) (See Why there is no something like IMonad<T> in upcoming .NET 4.0 for some more thoughts.)

    Since there is no way to represent that idea in the type system, you’re pretty much stuck with not using generics to solve this problem. The type system simply doesn’t support that level of genericity.

    People sometimes do horrid things like:

    static T Read<T>(string s)
    {
        if (typeof(T) == typeof(int)) return (T)(object)int.Parse(s);
        if ...
    

    but that is in my opinion a bit abusive; it really is not generic.

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

Sidebar

Related Questions

Have a interface class abc { public: virtual int foo() = 0; ... }
I have public class ListA { private string code; private string name; public string
I have two classes public class PrepaidPackage { private String name; private String serviceClassID;
I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
Let's say I have the following class: class ABC { private int myInt =
public class MyClass { ClassABC abc = new ClassABC(); } I just have a
I currently have a class of this form: class Abc { private readonly IDisposable
I have a class as follows: public class ABC { public IList<TextFillerDetail> TextFillerDetails {
public class ABC { public ABC(IEventableInstance dependency) { dependency.ANewEvent += MyEventHandler; } private void
Please consider following sample @Entity public class Abc { @Id private Long id; @Unindexed

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.