Methods and Different Kind of Parameters Passing


/ Published in: C#
Save to your folder(s)

In Delphi you have the possibility to modify Methods' Parameters (e.g. procedure AddPlus(var number: integer);) - in C# you will find the following realm of possibility:


Copy this code and paste it in your HTML
  1. using System;
  2.  
  3. namespace parameters
  4. {
  5. public static class democlass
  6. {
  7. //normal use
  8. public static int Add(int left, int right)
  9. {
  10. return left + right;
  11. }
  12.  
  13. //use the ref Parameter, e.g. for increment
  14. public static void AddPlus(ref int number)
  15. {
  16. number = number + 1;
  17. }
  18.  
  19. //use the out Parameter
  20. public static void GetHundred(out int number)
  21. {
  22. number = 100;
  23. }
  24. }
  25.  
  26. class Program
  27. {
  28. static void Main(string[] args)
  29. {
  30. int a = 10;
  31. int b = 20;
  32. int c;
  33.  
  34. c = democlass.Add(a, b);
  35. Console.WriteLine(c); //30
  36.  
  37. democlass.AddPlus(ref c);
  38. Console.WriteLine(c); //31
  39.  
  40. democlass.GetHundred(out c);
  41. Console.WriteLine(c); //100
  42. }
  43. }
  44. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.