How do I make the return type of a method generic?

Is there a way to make this method generic so I can return a string, bool, int, or double? Right now, it’s returning a string, but if it’s able find “true” or “false” as the configuration value, I’d like to return a bool for example. public static string ConfigSetting(string settingName) { return ConfigurationManager.AppSettings[settingName]; } 7 … Read more

What are the differences between Generics in C# and Java… and Templates in C++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers. Closed 8 years ago. Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. I mostly use Java and generics are relatively new. I keep … Read more

Pass An Instantiated System.Type as a Type Parameter for a Generic Class

The title is kind of obscure. What I want to know is if this is possible: string typeName = <read type name from somwhere>; Type myType = Type.GetType(typeName); MyGenericClass<myType> myGenericClass = new MyGenericClass<myType>(); Obviously, MyGenericClass is described as: public class MyGenericClass<T> Right now, the compiler complains that ‘The type or namespace ‘myType’ could not be … Read more

Generic TryParse

I am trying to create a generic extension that uses ‘TryParse’ to check if a string is a given type: public static bool Is<T>(this string input) { T notUsed; return T.TryParse(input, out notUsed); } this won’t compile as it cannot resolve symbol ‘TryParse’ As I understand, ‘TryParse’ is not part of any interface. Is this … Read more

The type must be a reference type in order to use it as parameter ‘T’ in the generic type or method

I’m getting deeper into generics and now have a situation I need help with. I get a compile error on the ‘Derived’ class below as shown in the subject title. I see many other posts similar to this one but I’m not seeing the relationship. Can someone tell me how to resolve this? using System; … Read more

How to reference generic classes and methods in xml documentation

When writing xml documentation you can use <see cref=”something”>something</see>, which works of course. But how do you reference a class or a method with generic types? public class FancyClass<T> { public string FancyMethod<K>(T value) { return “something fancy”; } } If I was going to write xml documentation somewhere, how would I reference the fancy … Read more