Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Generic constraints to include value types AND strings

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.

like image 576
Brett Postin Avatar asked Jan 05 '12 15:01

Brett Postin


People also ask

What C is used for?

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 ...

Is C language easy?

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.

What is the full name of C?

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.

Is C programming hard?

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.


2 Answers

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:

  • Boolean
  • Byte
  • Char
  • DateTime
  • Decimal
  • Double
  • Int (16, 32 and 64-bit)
  • SByte
  • Single (float)
  • String
  • UInt (16, 32 and 64-bit)

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.

like image 172
KeithS Avatar answered Sep 20 '22 08:09

KeithS


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) 
like image 41
Steven Avatar answered Sep 18 '22 08:09

Steven