Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - generic methods vs. non-generic methods

Tags:

c#

generics

I'm a bit confused about why/when I'd ever want to use a generic method since a non-generic method can access the generic members of its containing class and be passed generic arguments anyway.

So, using a canned example that likely misses the point (yet highlights why I'm asking this question), why would I do this:

public class SomeGeneric<T>
{
    public T Swap<T>(ref T a, ref T b)
    {
        T tmp = a;
        a = b;
        b = tmp;
    }
}

over

public class SomeGeneric<T>
{
    public T Swap(ref T a, ref T b)
    {
        T tmp = a;
        a = b;
        b = tmp;
    }
}

this?

Or, really, why would I want to use a generic method at all?

like image 477
Major Productions Avatar asked Dec 19 '12 19:12

Major Productions


People also ask

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.

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 do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

What is C language 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 ...


3 Answers

A common scenario for method level type parameters are extension methods because they must be declared in a non-generic static class. But they are required for every generic member in a non-generic type.

public static class Extensions
{
    public static void Foo<A, B>(this A a, B b) { [...] }

    public static T Bar<T>(this String input) { [...] }

    public static U FooBar<V, W>(this V v, W w) { [...] }
}
like image 101
Daniel Brückner Avatar answered Sep 21 '22 17:09

Daniel Brückner


You'd typically use a generic method in a type that isn't generic.

For example, look at the Enumerable class. It defines the generic extension methods for most of the LINQ fucntionaltiy, but itself isn't generic.

You also might want a generic method within a generic type, but only if the generic method used a different generic type specifier.

This lets you write something like the following:

 class Foo<T> where T : IConvertible, IComparable<T>
 {
      int CompareTo<U>(U other) where U : IConvertible
      {
           // Convert to this
           T otherConverted = Convert.ChangeType(other, typeof(T));
           return this.CompareTo(otherConverted);
      }
 }

(Granted, this is a bit contrived, but does compile and work correctly for Foo<int> comparing to a double, etc)

like image 20
Reed Copsey Avatar answered Sep 20 '22 17:09

Reed Copsey


What if the containing class is not generic? What if it has different generic type parameters?

like image 24
John Saunders Avatar answered Sep 21 '22 17:09

John Saunders