Warum passt das Muster -Matching in INT16 [] zu UINT16 [] in C#überein?C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Warum passt das Muster -Matching in INT16 [] zu UINT16 [] in C#überein?

Post by Anonymous »

Warum ist es, wenn ich in C#einen int16 [] einschalte, mit Uint16 [] übereinstimmt, selbst wenn ein int16 [] später in der Switch-Anweisung vorhanden ist?

Code: Select all

namespace SwitchError;

internal class Program
{
static void Main(string[] args)
{
Int16[] arr = new Int16[50];
Console.WriteLine($"Arr is {((arr is UInt16[]) ? " " : "not ")}UInt16[]");
Console.WriteLine($"Arr is {((arr is Int16[]) ? " " : "not ")}Int16[]");
PrintInfo(arr);
}

static void PrintInfo(T[] array)
{
string message = array switch
{
double[] b => "Double",
UInt16[] b => "Unsigned Int16",
Int16[] b => "Signed Int16",
_ => "Unknown type"
};

Console.WriteLine("Switch statement says it is an: " + message);
}
}
< /code>
Ausgabe: < /p>
Arr is not UInt16[]
Arr is  Int16[]
Switch statement says it is an: Unsigned Int16

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post