C# Adding two Generic Values

There is no generic constraint that allows you to enforce operator overload. You may take a look at the following library. Alternatively if you are using .NET 4.0 you could use the dynamic keyword: public static T Add<T>(T number1, T number2) { dynamic a = number1; dynamic b = number2; return a + b; } … Read more

Functions with generic parameter types

Overloading is typically the bugaboo of type-inferenced languages (at least when, like F#, the type system isn’t powerful enough to contain type-classes). There are a number of choices you have in F#: Use overloading on methods (members of a type), in which case overloading works much like as in other .Net languages (you can ad-hoc … Read more

Explanation why IEnumerable is more efficient than a List

IEnumerable<T> is an interface that is implemented by List<T>. I suspect the reason you’re hearing that IEnumerable<T> should be used is because it’s a less constrictive interface requirement. For example, consider the following method signature: void Output(List<Foo> foos) { foreach(var foo in foos) { /* do something */ } } This method requires that it … Read more

How to pass a type to generic method in Kotlin?

Here’s a complete example: import kotlin.reflect.KClass data class User(val name: String) data class Student(val name: String) fun getUsers(): List<User> = listOf(User(“JB”)) fun getStudents(): List<Student> = listOf(Student(“Claire”)) fun <T: Any> getData(clazz: KClass<T>): List<T>? { return when(clazz) { User::class -> getUsers() as List<T> Student::class -> getStudents() as List<T> else -> null } } fun main(args: Array<String>) { … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)