Hello,
I'm using generics in C# and I have several questions.
The above generates a compile-time error because .DataReturnMethod is "invisible", it's a method for a specific class such as...
So if I'm understanding this correctly, this behavior is prohibited because this would violate the reason for using generics in the first place? If I was using the example TestClass with int for example and not DataClass (ex: TestClass<int> as opposed to TestClass<DataClass>), it would be nonsense to call DataReturnMethod on an integer type since that method is undefined for the integer value type. Am I right or just wrong?
I'm using generics in C# and I have several questions.
Code:
class TestClass<T>
{
public T ReturnSomeData(T SomeArgument)
{
return SomeArgument.DataReturnMethod();
}
}
The above generates a compile-time error because .DataReturnMethod is "invisible", it's a method for a specific class such as...
Code:
class DataClass
{
private int Data;
public int DataReturnMethod()
{
return Data;
}
}
So if I'm understanding this correctly, this behavior is prohibited because this would violate the reason for using generics in the first place? If I was using the example TestClass with int for example and not DataClass (ex: TestClass<int> as opposed to TestClass<DataClass>), it would be nonsense to call DataReturnMethod on an integer type since that method is undefined for the integer value type. Am I right or just wrong?