We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

RichardIII on 02/11/08


Tagged

methods parameters passing out ref var


Versions (?)


Methods and Different Kind of Parameters Passing


Published in: C# 


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:


  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 

You need to login to post a comment.