1

I'm trying to output a value based on its type. Currently I'm creating a bunch of if-statements based on the type of T. Is there any better solution that makes the code look more concise?

    public static void Output<T>(T data)
    {
        string dataStr = data.ToString();

        if (typeof (T) == typeof(double))
        {
            dataStr = String.Format("{0:N4}", data);
        }

        Console.WriteLine(dataStr):
    }
derekhh
  • 5,272
  • 11
  • 40
  • 60

3 Answers3

3

The only alternative is to avoid generics and provide explicit method overloads based on type:

public static void Output(Double data) { Console.WriteLine( "{0:N4}", data ); }
public static void Output(String data) { Console.WriteLine(    "{0}", data ); }
public static void Output(Int32 data)  { Console.WriteLine(  "{0:G}", data ); }
// ...and so on

Note a downside to this approach is that overload selection is done at compile-time, not runtime, so this would fail: Output( (Object)someInt32Value );

Dai
  • 141,631
  • 28
  • 261
  • 374
0

it's batter option is Overloading,

Polymorphism:

When a message can be processed in different ways is called polymorphism. Polymorphism means many forms.

Polymorphism is one of the fundamental concepts of OOP.

Polymorphism provides following features:

It allows you to invoke methods of derived class through base class reference during runtime. It has the ability for classes to provide different implementations of methods that are called through the same name.

Polymorphism is of two types:

Compile time polymorphism/Overloading Runtime polymorphism/Overriding

Compile Time Polymorphism

Compile time polymorphism is method and operators overloading. It is also called early binding.

In method overloading method performs the different task at the different input parameters.

Runtime Time Polymorphism

Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding.

When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same prototype.

Caution: Don't confused method overloading with method overriding, they are different, unrelated concepts. But they sound similar.

Method overloading has nothing to do with inheritance or virtual methods.

1. More info click here, 2. More info click here

using System;

class Test
{
    static void Foo(int x, int y)
    {
        Console.WriteLine("Foo(int x, int y)");
    }

    static void Foo(int x, double y)
    {
        Console.WriteLine("Foo(int x, double y)");
    }

    static void Foo(double x, int y)
    {
        Console.WriteLine("Foo(double x, int y)");
    }

    static void Main()
    {
        Foo(5, 10);
    }
}
Nimesh Gami
  • 361
  • 1
  • 2
  • 18
0

One alternative would be maintaining a dictionary that matches Types to format strings.

The code is a little silly with one item, but any additional Types that need handling would only require an extra line in the dictionary:

public static void Output<T>(T data)
{
  var formats = new Dictionary<Type, string>
  {
    {typeof (Double), "{0:N4}"}
  };

  var format = formats.Where(x => x.Key == typeof(T))
                      .Select(x => x.Value).SingleOrDefault();

  string dataStr = data.ToString();

  if (format != null)
    dataStr = String.Format(format, data);

  Console.WriteLine(dataStr);
}

To give credit where it's due, a similar question with a similar response.

Community
  • 1
  • 1
gerg
  • 771
  • 3
  • 9
  • 18