Part 67 Optional parameters in c#

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ต.ค. 2024

ความคิดเห็น • 24

  • @Datta3
    @Datta3 2 หลายเดือนก่อน +1

    thank you sir

  • @Csharp-video-tutorialsBlogspot
    @Csharp-video-tutorialsBlogspot  11 ปีที่แล้ว +1

    Hi Prashanth, sure, I will as soon as I can.

  • @robertanic7799
    @robertanic7799 9 ปีที่แล้ว +1

    Excellent tutorial, easy to understand as usual. I liked each video :).

  • @martinendrst1410
    @martinendrst1410 11 ปีที่แล้ว +8

    Hello. First of all I would like to say that your tutorials are probably the best out there and thanks for the great work. I have a question tho. Why did you make the parameter array object type? Doesnt it suffer the two problems you explained on the Generics tutorial? Lack of Type Safety and Performance loss due to Boxing and Unboxing processes? Wouldnt it be possible and better to just type params int[] restOfNumbers? Thanks for clarification.

    • @swap2281997
      @swap2281997 8 หลายเดือนก่อน

      Correct! I too have the same question

  • @Tomy97xbox
    @Tomy97xbox 8 ปีที่แล้ว +4

    Also better to use params int[ ] to prevent string arguments.

    • @anthonwellsjo
      @anthonwellsjo 4 ปีที่แล้ว

      great, that's what I was about to ask for!

  • @ww1flyingace263
    @ww1flyingace263 4 ปีที่แล้ว +2

    As a side note, VB.NET uses the key word Optional for optional parameters. This is more elegant and straightforward.

  • @sendrelamaria3336
    @sendrelamaria3336 11 ปีที่แล้ว

    That's great. Many thanks.

  • @badisha
    @badisha 7 ปีที่แล้ว

    Mr. Venkat you are doing great. Could you please explain why you have used params object[] restOfNumber and not params int[] restOfNumbers? Thanks!!

    • @kylekastilahn
      @kylekastilahn 5 ปีที่แล้ว +1

      I think it's for flexibility. If you use object you can pass in short, double etc if you cast them before hand as int then you can use List for sum . Also without casting the arguments instead of looping with int if you loop with object you can first see what the type of the object is then handle accordingly. If you don't cast the method argument to int and use the List I'm pretty sure it will fail do to unboxing/boxing .
      static void Main(string[] args)
      {
      int i = 1;
      int i2 = 23;
      short s = 6;
      double d = 5.0;
      AddNumbers(i, i2);
      AddNumbers(i, s);
      AddNumbers(i, i2,8,9,10);
      AddNumbers(i, i2, (int)s, (int)d);
      //these willl fail do to unboxing if not handled correctly
      AddNumbers(i, i2, s);
      AddNumbers(i, i2, s, d);
      }
      public static void AddNumbers(int firstNumber, int secondNumber, params object[] restOfNumbers)
      {

      int [] nbrs = { 1, 2, 3 };
      List nAlso = nbrs.ToList();
      List test = new List() { 1, 2, 3 };
      List firstCoupleNumbers = new List();
      firstCoupleNumbers.Add(firstNumber);
      firstCoupleNumbers.Add(secondNumber);
      Console.WriteLine(firstCoupleNumbers.Sum());
      //List myParamsNbrs = restOfNumbers.Cast().ToList();
      //Console.WriteLine(myParamsNbrs.Sum());
      Console.WriteLine(restOfNumbers.Cast().ToList().Sum());

      Console.WriteLine("*******************************");

      }

  • @baseljuma158
    @baseljuma158 2 ปีที่แล้ว

    Is params must be an object[] or it can be any type array?

  • @Ayubajbnabi
    @Ayubajbnabi 11 ปีที่แล้ว

    Excellent.

  • @miquel2423
    @miquel2423 3 ปีที่แล้ว +1

    legend

  • @PraskYoutube
    @PraskYoutube 11 ปีที่แล้ว

    Hi Venkat, Please upload creation of Graphs in ASP.NET ,even for charts.i.e. Pie chart

  • @ufukdilek3550
    @ufukdilek3550 10 ปีที่แล้ว

    Thank you very much...

  • @rahulk4254
    @rahulk4254 3 ปีที่แล้ว

    Why place holder context don't work here when writing to Console. We are using the concatenate method instead.