473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Generic Parameter constraint - new()

So, we can use the new() constraint to specify that a given type must have a
parameterless constructor.

Why can't we say something like new(string) to specify that it must have a
parameter taking a single string? If we can do one, why not the other?

--
Adam Clauss

Aug 21 '06 #1
1 1851
Because one is more complex than the other, and new() satisfies a *lot*
of classes? I really do feel your pain though.

I can only think of two ways of doing this:
1: reflection; in the static ctor for the class, use reflection to find
(and cache) the specific ctor; if there isn't one, throw an exception;
else (later) use the cached ctor to create objects [not compile-time
safety, but the next best thing]
2: delegation; create a delegate T Constructor<T>(string value) that
must be passed to the method, and use that internally? Again, not
ideal, but it can work (and also supports factory-pattern usage with
deferred evaluation, which is quite nice).

Marc

Aug 22 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dmitry Martynov | last post by:
What if I have some special constructors and do not have a default constructor. It seems to be very useful when I do not want to forget to initialize some fields properly. As I have understood...
6
by: Robert Zurer | last post by:
This method works but FxCop rightly complains "Generic methods should provide type parameter" http://www.gotdotnet.com/team/fxcop/docs/rules.aspx?version=1.32...
4
by: Jethro Guo | last post by:
C++ template use constraint by signature,It's very flexible to programmer but complex for complier, and at most time programmer can not get clear error message from complier if error occur. C#...
4
by: Andreas Mueller | last post by:
Hi all, I have a method where the compiler is unable to deduce the generic arguments. Now I'm asking myself if this a bug, or if not, why it isn't working. The method in question is Foo3: ...
6
by: Dan Holmes | last post by:
I have a class that i need a constraint of int, string, float or bool. I have tried the following but can't make VS accept it. I read the docs and they showed that any value type can be used...
3
by: BBM | last post by:
Hi, I have a situation where I would like to extend an abstract (MustInherit) class that uses a Generic parameter. The class declaration looks like this... Public MustInherit Class...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
2
by: gilbert | last post by:
Hello. I am trying to use c# generic to define a class. class MyClass<Twhere T: new(){ } In this definition, MyClass can create T objects with a default constructor. Is there any way to...
9
by: Andrus | last post by:
I need to create instance of generic type when generic type does not have parameterless constructor. I tried code below but got error shown in comment. How to fix ? Andrus. abstract class...
9
by: tadmill | last post by:
Is it possible to pass a generic parameter of the same class to to its constructor, where the "T" type passed in the constructor is different than the "T" type of the instanced class? ie, ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.