C
C#

help

✅ Simple question

GGroophy2/11/2023
I have a code which
if ("any" is "item1" or "item2" or "any")
{
Console.WriteLine("okay");
}
if ("any" is "item1" or "item2" or "any")
{
Console.WriteLine("okay");
}
that prints okay but out of curiosity, can I do this with symbols? like
if ("any" == "item1" || "item2" || "any") // "any" == "item1" || "any" == "item2" will be corrent but really bad readable
{
Console.WriteLine("okay");
}
if ("any" == "item1" || "item2" || "any") // "any" == "item1" || "any" == "item2" will be corrent but really bad readable
{
Console.WriteLine("okay");
}
Eero2/11/2023
Well, can you do it? Have you tried? Did it work?
JJansthcirlU2/11/2023
The first code block is using pattern matching, it's a specific feature where you can use or to test against for different matches. The second code block uses the binary conditional logic operators which only work on booleans (true or false).
TTheBoxyBear2/11/2023
Only works with bools and it so happens the logical and equality operators return bool. Pattern matching helps make it more readable and can warn you if the expression is contradictory or will always be true. As a side effect, pattern matching only works when comparing to constants.
Ccumslvt132/11/2023
The code you've shown compiles into plain ||'s. Take a look at sharplab decompile:
internal class Program
{
private static void <Main>$(string[] args)
{
string text = "any";
if (text == "item1" || text == "item2" || text == "any")
{
Console.WriteLine("okay");
}
}
}
internal class Program
{
private static void <Main>$(string[] args)
{
string text = "any";
if (text == "item1" || text == "item2" || text == "any")
{
Console.WriteLine("okay");
}
}
}
https://sharplab.io/#v2:CYLg1APgAgTADAWAFCwIzOQNwIYCcAE2+AvPgETYB2AnmQNwZICWAZvgBRFMDO5TALgFMAtqjL4A9gTICRMcVPJVaASmQBvZPm34oqAJzsyEgNbZVDJAF8gA
SharpLab
C#/VB/F# compiler playground.
GGroophy2/11/2023
Thanks for best explain. $close
MMODiX2/11/2023
Use the /close command to mark a forum thread as answered

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Hi im not sure that my code is working as it should(class in c sharp)providing code here👇(When i add +1 or +10 to the GetX or GetY isn't it supossed to change?)❔ VISUAL STUDIO C# DATABASE AND LISTBOXI am currently working on my assignment and am stuck on this issue Where I have a Database to fetch❔ Sprites are invisible in build?i can see everything in the engine (unity) and ive gone over a bunch of things even checking my came❔ Hi,my flappy bird game script isnt workingThe script in making is to show that the score counter incrreases when the gamobject(bird) collides Hi i need some help with classes, im pretty new to this and i have an error with my code.help would be appreciated(in csharp)❔ Optional does not work with C# protobufsI have a proto buf with the timestamp field being marked as optional, but it doesn't seem to do anyt❔ ✅The largest inscribed circle inside concave (non-convex) polygon 2DHey, I want to make an algorithm that will return the largest inscribed circle in any 2D polygon (or❔ SqlLite Datareader to C# ModelWhat is the best way to convert the datareader object to the c# model class?❔ Program does not contain static 'Main'```csharp using System.Collections.Specialized; using System.Security.Cryptography.X509Certificates;❔ Real-Time MSTest ReporterI'm wanting to write a custom, real-time reporter for MSTest. Is there a formal API for hooking into❔ If my microservices are bunch of azure function should I use rabbitmq ?Hi I started to hear about rabbitmq more frequently but the way I communicate with services Azure Fu✅ Tuples in classes```csharp using System.Collections.Specialized; class Program { static void Main(string[] args)✅ Check a param againstt a stringI'm creating a sort of Animal Dictionary and I need help with check if the name param in the AddToSe✅ ✅ Use function that has default implementation in interface from instance of classI have interface similar to this: ```cs interface IFoo { void DoThing1(); void DoThing2(); vo❔ process.start() works in debug not in iisHello every one. I have an application on iis. The application should run an exe with process start(❔ `delegate*` wrapper throws `System.BadImageFormatException`I wrote some wrapper for `delegate* unmanaged` to use it in F# since it doesn't support function poi✅ Does azure function really cheaper than app service when trigger http ?Hi I understand that the way azure function charge is based on requests not like appservice 24/24 b❔ Any good persistent data solution for .net core 6 c#?Back in .NET framework I use settings and the ``Properties.Settings.Default`` namespace to store dat