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

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Warum passt das Muster -Matching in INT16 [] zu UINT16 [] in C#überein?

by Anonymous » 03 Apr 2025, 07:37

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

Top