开发者

why assign null value or another default value firstly?

i try to generate some codes. i face to face delegates. Everythings is ok.(Look below) But appearing a warning: you shold assing value why? but second code below is ok.

namespace Delegates
{
    class Program
    {
        static void Main(string[] args)
        {

        HesapMak hesapla = new HesapMak();
        hesapla.Calculator = new HesapMak.Hesap(hesapla.Sum);

        double sonuc = hesapla.Calculator(34, 2);
        Console.WriteLine("Toplama Sonucu:{0}",sonuc.ToString());
        Console.ReadKey();
    }
}

class HesapMak
{
   public double Sum(double s1, double s2)
    {
        return s1 + s2;
    }

   public double Cikarma(double s1, double s2)
    {
        return s1 - s2;
    }

   public double Multiply(double s1, double s2)
    {
        return s1 * s2;
    }
   public double Divide(double s1, double s2)
    {
        return s1 / s2;
    }
      public delegate double Hesap(double s1, double s2);
      public Hesap Calculator; ----< they want me assingn value

}

}

namespace Delegates { class Program { static void Main(string[] args) { HesapMak hesapla = new HesapMak(); hesapla.Calculator = new HesapMak.Hesap(hesapla.Sum); double sonuc = hesapla.Calcula开发者_开发技巧tor(34, 2); Console.WriteLine("Toplama Sonucu:{0}",sonuc.ToString()); Console.ReadKey(); } } class HesapMak { public double Sum(double s1, double s2) { return s1 + s2; } public double Cikarma(double s1, double s2) { return s1 - s2; } public double Multiply(double s1, double s2) { return s1 * s2; } public double Divide(double s1, double s2) { return s1 / s2; } public delegate double Hesap(double s1, double s2); public Hesap Calculator=null; } }


Your class HesapMak is internal. The compiler can therefore see every use of the field Calculator immediately.

If nowhere in your code you assign to Calculator, the compiler will give you a warning. This is because the field is then unnecessary.

The warning will go away as soon as you start using the field somewhere in your code. So just ignore it for now and write on.


This is not neccessary and should not result in a warning.
Section 10.4.4 of the C# (3.0) spec says:

The initial value of a field, whether it be a static field or an instance field, is the default value (Section 5.2) of the field's type. It is not possible to observe the value of a field before this default initialization has occurred, and a field is thus never "uninitialized". The following categories of variables are automatically initialized to their default values:

Section 5.2 of the C# (3.0) spec says:

The default value of a variable depends on the type of the variable and is determined as follows:

  • For a variable of a value-type, the default value is the same as the value computed by the value-type's default constructor (Section 4.1.1).
  • For a variable of a reference-type, the default value is null.

So public Hesap Calculator; is initialized automatically to its default value (null).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜