anjinkristou

Variable argument in C#

Jun 26th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.49 KB | None | 0 0
  1. The params keyword can be applied on a method parameter that is an array.  When the method is invoked, the elements of the array can be supplied as a comma separated list.
  2.  
  3. So, if the method parameter is an object array,
  4.  
  5. void paramsExample(object arg1, object arg2, params object[] argsRest)
  6. {
  7. foreach (object arg in argsRest)
  8. { /* .... */ }
  9. }
  10.  
  11. then the method can be invoked with any number of arguments of any type.
  12. paramsExample(1, 0.0f, "a string", 0.0m, new UserDefinedType());
Advertisement
Add Comment
Please, Sign In to add comment