Provide different formats based on input types in C#

derekhh

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):
    }
Dai

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 );

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Provide different formats based on input types in C#

From Dev

Python TypeHint a function that returns different types based on input

From Dev

Python TypeHint a function that returns different types based on input

From Dev

Resolving to different types based on the argument types of a c++ template function

From Dev

C# number different formats

From Dev

Different input types for fscanf

From Dev

Different validators based on input

From Dev

HiveQL converting or synchronizing date types stored in different formats

From Dev

Storing input as different types in Java

From Dev

Different input types in the same method

From Dev

Validate different date formats in C#

From Dev

c variadic functions, same arguments, different formats

From Dev

how to send two different types of JSON response for a same request based on the input REST?

From Dev

is that possible to define different type of variable based on the input variable in c++?

From Dev

C++ Using different subclasses based on part of input

From Dev

C++ provide only undeducable template types

From Dev

C++ provide only undeducable template types

From Dev

Different Java Scanner for input of different types

From Dev

Using NodaTime to parse an input and output different dateTime formats

From Dev

Using NodaTime to parse an input and output different dateTime formats

From Dev

How Can I Apply Different Formats Based on Data Values

From Dev

How Can I Apply Different Formats Based on Data Values

From Dev

Searching for keys in a Scala Map based on different keyword formats

From Dev

Piping to provide a file as input to a C program

From Dev

Provide hex value as an input to gets in C

From Dev

Piping to provide a file as input to a C program

From Dev

Why can't you use scanf() in C for multiple input arguments of different types?

From Dev

C Data Types reading different input from stdin and printing altered data

From Dev

Provide different constructor arguments to a common dependency based on dependant class

Related Related

  1. 1

    Provide different formats based on input types in C#

  2. 2

    Python TypeHint a function that returns different types based on input

  3. 3

    Python TypeHint a function that returns different types based on input

  4. 4

    Resolving to different types based on the argument types of a c++ template function

  5. 5

    C# number different formats

  6. 6

    Different input types for fscanf

  7. 7

    Different validators based on input

  8. 8

    HiveQL converting or synchronizing date types stored in different formats

  9. 9

    Storing input as different types in Java

  10. 10

    Different input types in the same method

  11. 11

    Validate different date formats in C#

  12. 12

    c variadic functions, same arguments, different formats

  13. 13

    how to send two different types of JSON response for a same request based on the input REST?

  14. 14

    is that possible to define different type of variable based on the input variable in c++?

  15. 15

    C++ Using different subclasses based on part of input

  16. 16

    C++ provide only undeducable template types

  17. 17

    C++ provide only undeducable template types

  18. 18

    Different Java Scanner for input of different types

  19. 19

    Using NodaTime to parse an input and output different dateTime formats

  20. 20

    Using NodaTime to parse an input and output different dateTime formats

  21. 21

    How Can I Apply Different Formats Based on Data Values

  22. 22

    How Can I Apply Different Formats Based on Data Values

  23. 23

    Searching for keys in a Scala Map based on different keyword formats

  24. 24

    Piping to provide a file as input to a C program

  25. 25

    Provide hex value as an input to gets in C

  26. 26

    Piping to provide a file as input to a C program

  27. 27

    Why can't you use scanf() in C for multiple input arguments of different types?

  28. 28

    C Data Types reading different input from stdin and printing altered data

  29. 29

    Provide different constructor arguments to a common dependency based on dependant class

HotTag

Archive