title: delegate function does not see class object
body: <p>I was trying to do a delegate example by myself. The delegate name is <code>MaaşKarşılaştırmaDelegesi</code>. First, I tried to compare two objects' fields (which is "maaş") from the same class (which are either <code>BeyazYaka</code> or <code>MaviYaka</code>), it was working well. But then I wanted to compare two objects from different classes( one from <code>MaviYaka</code> and one from <code>BeyazYaka</code>) but then, inside the delegated function in the Program class, the compiler gives an error saying:</p>
  
  <blockquote>
    <p>The name 'k1' does not exist in the current context</p>
    
    <p>The name 'k2' does not exist in the current context</p>
  </blockquote>
  
  <p>So the compiler does not see the created class objects. I think the error is in the if else block. Because when i try to remove the block and just write;</p>
  
  <pre><code>MaviYaka k1 = (MaviYaka)o1;
  
  BeyazYaka k2 = (BeyazYaka)o2;
  </code></pre>
  
  <p>the error is removed. But it gives error when this is in the if block.
  Can you help me why this happens?</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace tekrar_delegates
  {
      class MaviYaka
      {
      public int maaş;
  
      public MaviYaka(int maaş)
      {
          this.maaş = maaş;
      }
  
      public static int KiminMaaşıDahaFazla(object o1, object o2) 
      {
          MaviYaka m1 = (MaviYaka)o1;
          MaviYaka m2 = (MaviYaka)o2;
  
          if (m1.maaş &gt; m2.maaş) { return 1; }
          else { return 2; }
      }
  
  }
  
  class BeyazYaka
  {
      public int maaş;
  
      public BeyazYaka(int maaş)
      {
          this.maaş = maaş;
      }
  
      public static int KiminMaaşıDahaFazla(object o1, object o2) 
      {
          BeyazYaka b1 = (BeyazYaka)o1;
          BeyazYaka b2 = (BeyazYaka)o2;
  
          if (b1.maaş &gt; b2.maaş) { return 1; }
          else { return 2; }
      }
  }
  
  class Pair
  {
      private object[] thePair = new object[2];
  
      public Pair(object o1, object o2)
      {
          thePair[0] = o1;
          thePair[1] = o2;
      }
  
      public delegate int MaaşKarşılaştırmaDelegesi(object o1, object o2);
  
      public void Print(MaaşKarşılaştırmaDelegesi delegatedFunc)
      {
          if (delegatedFunc(thePair[0], thePair[1]) == 1) { Console.WriteLine("1.nin maaşı daha büyük."); }
          else { Console.WriteLine("2.nin maaşı daha büyük."); }
      }
  
  }
  
  class Program
  {
      public static int KiminMaaşıDahaFazla(object o1, object o2)
      {
          if (true)
          {
              MaviYaka k1 = (MaviYaka)o1;
          }
          else { }
          if (true)
          {
              BeyazYaka k2 = (BeyazYaka)o2;
          }
          else { }
  
          if (k1.maaş &gt; k2.maaş) { return 1; }
          else { return 2; }
      }
  
  
  
      static void Main(string[] args)
      {
          MaviYaka maviYaka1 = new MaviYaka(300);            
  
          BeyazYaka beyazYaka1 = new BeyazYaka(500);            
  
          Pair karışıkPair = new Pair(maviYaka1, beyazYaka1);            
  
          Pair.MaaşKarşılaştırmaDelegesi karışıkDelege = new Pair.MaaşKarşılaştırmaDelegesi(KiminMaaşıDahaFazla);
  
          karışıkPair.Print(karışıkDelege);     
  
          Console.Read();
      }
  }
  </code></pre>