L
L
Levingstone2020-10-11 06:43:45
C++ / C#
Levingstone, 2020-10-11 06:43:45

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

4 answer(s)
I
Ilya, 2020-10-11
@Levingstone

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

D
Dmitry Pavlov, 2020-10-11
@Stalker31

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

V
Vasily Bannikov, 2020-10-12
@vabka

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

M
MaksSmag, 2020-10-11
@MaksSmag

In the tutorials, they managed to write a whole chapter about overload. Why don't you like it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question