I'm trying to write an extension method on IEnumerable that will only apply to value types and strings.
public static string MyMethod<T>(this IEnumerable<T> source) where T : struct, string
However 'string' is not a valid constraint as it is a sealed class.
Is there any way to do this?
Edit:
What I'm actually trying to do is prepare a list of values for an "IN" clause in a dynamically constructed SQL.
I have lots of instances of code such as the following that I want to clean up:
sb.AppendLine(string.Format("AND value IN ({0})", string.Join(",", Values.Select(x => x.ToSQL()).ToArray())));
Where ToSQL() has code to handle SqlInjection.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.
Maybe you could restrict to IConvertible types? All the system primitives that can be converted using these interface methods also implement the interface, so this restriction would require T to be one of the following:
If you have an IConvertible, chances are VERY good it's one of these types, as the IConvertible interface is such a pain to implement that it's rarely done for third-party types.
The main drawback is that without actually converting T to an instance of one of these types, all your method will know how to do is call the Object and IConvertible methods, or methods that take an Object or IConvertible. If you need something more (like the ability to add and/or concatenate using +), I think that simply setting up two methods, one generic to struct types and a second strongly-typed to strings, would be the best bet overall.
You need to define 2 separate methods:
public static string MyMethod<T>(this IEnumerable<T> source) where T : struct public static string MyMethod(this IEnumerable<string> source)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With