How to check if the result of an expression is compatible with a given type with operator is in C#

1 Answer

0 votes
using System;

public struct Worker
{
   public string Name { get; set; }

   public Worker(string name) : this() {
      Name = name;
   }
}

public class Program
{
   public static void Print(object o) {
      if (o is Worker p) {
         Console.WriteLine(p.Name);
      }
   }
   public static void Main()
   {
      Object o = new Worker("Tom");
      Print(o);
   }


}


 
   
   
   
/*
run:
   
Tom
   
*/

 



answered Nov 26, 2020 by avibootz

Related questions

...