Answer the question
In order to leave comments, you need to log in
Does C# | — or between types?
Tell me please. Does the bitwise operator | | work in C#? for types, as in typescript.
That is, can I somehow indicate that two different types can come into the method, for example ??
public static void Main(string[] | number[] args)
{
//Your code goes here
Console.WriteLine("Hello, world!");
}
Answer the question
In order to leave comments, you need to log in
No, not yet. It may appear in the future. In the case of Main, you need to take an array of strings and parse them into the desired type. In other cases, you can use object[] or ArrayList and cast each element to the desired type
Method overloading is not for you?
public static void Main(string[] args)
{
//Your code goes here
W(0);//передаём int
W("string");//передаём string
}
void W(string t){
Console.WriteLine(t);
}
void W(int t){
Console.WriteLine(t.ToString());
}
In the future, most likely, there will be Discriminated unions, but now there is no such thing.
As alternatives:
1. Method overloading
2. Polymorphism (in this case, accept object[] and do switch(x) { case string s: ...; case object o: ...} in the loop
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question