Advertisement
Caminhoneiro

Params[]

Jul 17th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1.   [TestClass]
  2.     public class ParamsExample
  3.     {
  4.  
  5.         private void WriteDebug(string message, params object[] objects)
  6.         {
  7.             Debug.WriteLine(message);
  8.  
  9.             foreach (var o in objects)
  10.             {
  11.                 Debug.WriteLine(o);
  12.             }
  13.         }
  14.  
  15.        
  16.         [TestMethod]
  17.         public void WriteDebugExample()
  18.         {
  19.             WriteDebug("* Some different objects *", 42, new Uri("http://pluralsight.com"), true);
  20.  
  21.             WriteDebug("* No objects *");
  22.         }
  23.  
  24.  
  25.  
  26.         private IEnumerable<int> CalcStringLengths(params string[] strings)
  27.         {
  28.             foreach (var s in strings)
  29.             {
  30.                 yield return s.Length;
  31.             }
  32.         }
  33.  
  34.  
  35.         [TestMethod]
  36.         public void CalcStringLengthsExample()
  37.         {
  38.             foreach (var len in CalcStringLengths("a", "aa", "aaa"))
  39.             {
  40.                 Debug.WriteLine(len);
  41.             }
  42.         }
  43.  
  44.  
  45.  
  46.         [TestMethod]
  47.         public void CalcStringLengthsExampleWithArray()
  48.         {
  49.             var names = new[] { "Sarah", "Gentry", "Angelina" };
  50.  
  51.             foreach (var len in CalcStringLengths(names))
  52.             {
  53.                 Debug.WriteLine(len);
  54.             }
  55.         }
  56.  
  57.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement