none
switch over a type RRS feed

  • Question

  • how to do something like so:

    public static bool IsCoincidence<T>(T param1, T param2)
            {
                switch (T) // error
                {
                    case SimplePoint _:
    
                    default:
                        break;
                }
    }
    thanks in advance.

    Saturday, February 22, 2020 7:53 AM

Answers

  • Newer C# features make this simple:

    public static void Test<T>(T value)
    {
    	switch (value)
    	{
    		case int i:
    			MessageBox.Show("value is int");
    			break;
    		case double d:
    			MessageBox.Show("value is double");
    			break;
    		case Form f:
    			MessageBox.Show("value is Form");
    			break;
    		default:
    			MessageBox.Show("value is unknown");
    			break;
    	}
    }
    


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    • Marked as answer by essamce Saturday, February 22, 2020 7:02 PM
    Saturday, February 22, 2020 2:52 PM
  • If param1 is never null, you can use this construct:

       switch( param1 )

       {

       case SimplePoint _:

          . . .

     

    • Marked as answer by essamce Saturday, February 22, 2020 7:02 PM
    Saturday, February 22, 2020 3:06 PM

All replies

  • Try this approach:

    public static bool IsCoincidence( SimplePoint param1, SimplePoint param2 )
    {
       . . .
    }
     
    public static bool IsCoincidence( ComplexPoint param1, ComplexPoint param2 )
    {
       . . .
    }
     
    public static bool IsCoincidence<T>( T param1, T param2 )
    {
       // other cases
       . . .
    }
    


    Saturday, February 22, 2020 9:30 AM
  • hi Viorel, thanks for repleying

    i don't want to use overloaddng i just want to know how to use switch case.

    Saturday, February 22, 2020 1:59 PM
  • Newer C# features make this simple:

    public static void Test<T>(T value)
    {
    	switch (value)
    	{
    		case int i:
    			MessageBox.Show("value is int");
    			break;
    		case double d:
    			MessageBox.Show("value is double");
    			break;
    		case Form f:
    			MessageBox.Show("value is Form");
    			break;
    		default:
    			MessageBox.Show("value is unknown");
    			break;
    	}
    }
    


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    • Marked as answer by essamce Saturday, February 22, 2020 7:02 PM
    Saturday, February 22, 2020 2:52 PM
  • If param1 is never null, you can use this construct:

       switch( param1 )

       {

       case SimplePoint _:

          . . .

     

    • Marked as answer by essamce Saturday, February 22, 2020 7:02 PM
    Saturday, February 22, 2020 3:06 PM