Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is GenericParameterHelper and how is it used?

Tags:

.net

I generated unit tests on a generic class in VS 2008 and it used the type GenericParameterHelper in all of the places that a generic type was used. Is this a placeholder that should be replaced or does it have some use? What are the uses if any?

Here's one of the tests it generated as an example:

/// <summary>
///A test for Count
///</summary>
public void CountTestHelper<TKey, TValue>()
{
    ObservableDictionary<TKey, TValue> target = new ObservableDictionary<TKey, TValue>(); // TODO: Initialize to an appropriate value
    int actual;
    actual = target.Count;
    Assert.Inconclusive("Verify the correctness of this test method.");
}

[TestMethod()]
public void CountTest()
{
    CountTestHelper<GenericParameterHelper, GenericParameterHelper>();
}
like image 436
Bryan Anderson Avatar asked Oct 28 '09 20:10

Bryan Anderson


People also ask

What is a generic method and when should it be used?

Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type. If there isn't such a dependency, a generic method should not be used. It is possible to use both generic methods and wildcards in tandem.

What is a generic parameter in Java?

Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.

What is a generic type parameter?

Generic Methods A type parameter, also known as a type variable, is an identifier that specifies a generic type name. The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.

Can you give an example of a generic method?

Example: Create a Generics Method Here, the type parameter <T> is inserted after the modifier public and before the return type void . We can call the generics method by placing the actual type <String> and <Integer> inside the bracket before the method name. demo. <String>genericMethod("Java Programming"); demo.


1 Answers

Let's say you have a class:

public class Foo<T>
{
    public bool DoSomething()
    {
        return false;
    }

    public T DoSomethingElse()
    {
    // ...
}

Now you want to test DoSomething. First you have to instantiate Foo. You can't do:

var foo = new Foo<T>();

You have to use a real type. But T isn't used in the method, so it's noise in the test. So you can do:

var foo = new Foo<GenericParameterHelper>();

...which stands, more or less, for "any old type."

like image 190
Craig Stuntz Avatar answered Sep 29 '22 17:09

Craig Stuntz