PageRenderTime 70ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/DynamicSugar.Console/index.sample.cs

http://github.com/fredericaltorres/DynamicSugarNet
C# | 266 lines | 226 code | 35 blank | 5 comment | 3 complexity | fa6a58fc9220fe318c54b6d305931f1c MD5 | raw file
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using DynamicSugar;
  6. using System.Dynamic;
  7. #if WINDOWS
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9. #endif
  10. namespace N {
  11. public class samples {
  12. static void DoSomething(List<int> l){
  13. }
  14. static void Sample2(string [] args) {
  15. var i = 3;
  16. var Values = new List<int>() { 1, 2, 3 }; // Regular C#
  17. if(Values.Contains(i))
  18. Console.WriteLine("in the list");
  19. if(i.In(1,2,3)) // Dynamic Sugar Syntax
  20. Console.WriteLine("in the list");
  21. }
  22. void MyFunction(IDictionary<string, object> d){
  23. }
  24. void Sample3(){
  25. MyFunction(new Dictionary<string,object>() { // Regular C#
  26. { "Debug" , true },
  27. { "Continent", "North America" }
  28. });
  29. // Dynamic Sugar Syntax
  30. MyFunction( DS.Dictionary( new { Debug=true, Continent="North America" } ) );
  31. // In() method
  32. var stones = DS.Dictionary<int>( new { Mick=1943, Keith=1943, Brian=1942, Bill=1936, Charlie=1941 } );
  33. if("Mick".In(stones)) {
  34. }
  35. }
  36. void MyFunction( IDictionary<string, bool> d){
  37. }
  38. void Sample4(){
  39. MyFunction(new Dictionary<string, bool>() { // Regular C#
  40. { "Debug" , true },
  41. { "Verbose", false }
  42. });
  43. // Dynamic Sugar Syntax
  44. MyFunction( DS.Dictionary<bool>( new { Debug=true, Verbose=false } ) );
  45. }
  46. // Returning anonymous types from a method and accessing members without reflection.
  47. void Sample5(){
  48. var firstName = "Fred";
  49. var age = 45;
  50. var s1 = String.Format("firstName:{0}, age:{1}", firstName, age ); // Regular C#
  51. // Dynamic Sugar Syntax. Is that beautiful?
  52. var s2 = "firstName:{firstName}, age:{age}".Template(new { firstName, age });
  53. }
  54. class Person {
  55. public string Name { set; get; }
  56. public int Age { set; get; }
  57. public string Format(string format, params object[] args) {
  58. return DynamicSugar.ExtendedFormat.Format(this, format, args);
  59. }
  60. }
  61. void Sample6(){
  62. var p = new Person() { Name = "Fred", Age = 45 };
  63. Console.WriteLine( String.Format( "Name={0}, Age={1:000}", p.Name, p.Age ) ); // Regular C#
  64. Console.WriteLine( p.Format( "Name={Name}, Age={Age:000}" ) ); // Dynamic Sugar Syntax
  65. }
  66. public static void Sample6_2(){
  67. var v1 = DS.List(1, 2, 3).Format(); // 1, 2, 3
  68. var v2 = DS.List(1, 2, 3).Format( @"""{0}""", "; " ); // "1"; "2"; "3"
  69. var v3 = DS.List("a", "b", "c").Format(); // "a", "b", "c"
  70. var v4 = DS.List("a", "b", "c").Format( @"({0})", "; " ); // ("a"); ("b"); ("c")
  71. var beatles = DS.Dictionary<int>( new { Paul=1942, John=1940, Richard=1940, George=1943 } );
  72. var v5 = beatles.Format(); // { Paul:1942, John:1940, Richard:1940, George:1943 }
  73. var v6 = beatles.Format(@"""{0}""=""{1}""", "; "); // { "Paul"="1942"; "John"="1940"; "Richard"="1940"; "George"="1943" }
  74. }
  75. // Regular C#
  76. static bool ComputeValues(int value, out double amount, out string message){
  77. amount = 2.0 * value;
  78. message = "Hello";
  79. return true;
  80. }
  81. // Dynamic Sugar Syntax
  82. static dynamic ComputeValues(int value){
  83. return DS.Values( new { returnValue = true, amount = 2.0 * value, message = "Hello" } );
  84. }
  85. void Sample7(){
  86. // Regular C#
  87. string message;
  88. double amount;
  89. var returnStatus = ComputeValues( 2, out amount, out message );
  90. Console.WriteLine( "returnStatus:{0}, amount:{1}, message:{2}", returnStatus, amount, message );
  91. // Dynamic Sugar Syntax
  92. var r = ComputeValues( 2 );
  93. Console.WriteLine("returnStatus:{0}, amount:{1}, message:{2}", r.returnValue, r.amount, r.message );
  94. }
  95. void Sample8(){
  96. for(int i=0; i<10; i++) // Regular C#
  97. Console.WriteLine(i);
  98. foreach(var i in DS.Range(10)) // Dynamic Sugar Syntax
  99. Console.WriteLine(i);
  100. }
  101. public static void Sample9(){
  102. var v1 = DS.List(1, 2, 3).Add( DS.List( 4, 5, 6 ) );
  103. var v2 = DS.List(1, 2, 3).Clone();
  104. var v3 = DS.List(1, 2, 3).Filter( e => e % 2 == 0 ); // Same as FindAll() for IEnumerable<>
  105. var v5 = DS.List(1, 2, 3).Identical( DS.List( 1, 2, 3 ) );
  106. var v6 = DS.List(1, 2, 3).Include( DS.List( 2, 3 ) );
  107. var v7 = DS.List(1, 2, 3).Include( 2, 3 );
  108. var v8 = DS.List(1, 2, 3).Intersect( DS.List( 3, 4, 5) ); // Same as Intersect() for IEnumerable<>
  109. var v9 = DS.List(1, 2, 3).IsNullOrEmpty();
  110. var v10 = DS.List(1, 2, 3).Map( e => e * 2 ); // Same as Select() for IEnumerable<>
  111. var v11 = DS.List(1, 2, 3).Merge( DS.List(3, 4, 5) );
  112. var v12 = DS.List(1, 2, 3).Reject( e => e % 2 == 0 );
  113. var v13 = DS.List(1, 2, 3).Substract( DS.List( 3, 4, 5) );
  114. DS.List(1, 2, 3).ToFile(@"C:\MyFile.txt");
  115. var v15 = DS.ListHelper.FromFile<int>(@"C:\MyFile.txt");
  116. var v16 = DS.List(1, 2, 3).Without( DS.List(2, 3) );
  117. var v17 = DS.List(1, 2, 3).Without( 2, 3 );
  118. }
  119. void Sample10(){
  120. var v1 = DS.List(1, 2, 3);
  121. while(!v1.IsEmpty()) {
  122. Console.WriteLine("{0} - {1}", v1.First(), v1.Last());
  123. v1 = v1.Rest();
  124. }
  125. }
  126. public void Sample11() {
  127. var s = "ABCD";
  128. #if WINDOWS
  129. Assert.AreEqual("ABCD" , s.Slice(0 ));
  130. Assert.AreEqual("BCD" , s.Slice(1 ));
  131. Assert.AreEqual("AB" , s.Slice(0 , 2));
  132. Assert.AreEqual("BC" , s.Slice(1 , 2));
  133. Assert.AreEqual("DCBA" , s.Slice(-1 ));
  134. Assert.AreEqual("CB" , s.Slice(-2, 2));
  135. #endif
  136. }
  137. public static void Sample12() {
  138. #if WINDOWS
  139. Assert.AreEqual("DCBA", "ABCD".Reverse());
  140. Assert.AreEqual("1;2;3", ";".Join(DS.List(1, 2, 3)));
  141. string s1 = null;
  142. Assert.AreEqual("default", s1.IfNullOrEmpty("default"));
  143. if(s1.IsNullOrEmpty()){
  144. }
  145. Assert.AreEqual("Lastname", "lastname".Capitalize());
  146. Assert.AreEqual("1,2,3,", ",1,2,3,".RemoveFirstChar());
  147. Assert.AreEqual("1,2,3,", ",1,2,3,".RemoveFirstChar(','));
  148. Assert.AreEqual(",1,2,3", ",1,2,3,".RemoveLastChar());
  149. Assert.AreEqual(",1,2,3", ",1,2,3,".RemoveLastChar(','));
  150. Assert.AreEqual("1,2,3", ",1,2,3,".RemoveLastChar().RemoveFirstChar());
  151. #endif
  152. }
  153. public static void Sample13() {
  154. var beatles = DS.Dictionary<int>( new { Paul=1942, John=1940, Richard=1940, George=1943 } );
  155. var other = beatles.Get("Pete", 1941); // Support a default value for non existing key
  156. var younger1 = beatles.Max(); // Return the entry with the greatest value
  157. var younger2 = beatles.Max(DS.List("Paul", "John", "Richard"));
  158. var older1 = beatles.Min(); // Return the entry with the smallest value
  159. var older2 = beatles.Min(DS.List("Paul", "Richard", "George"));
  160. var newMusicians = beatles.Clone();
  161. // Add or remove entries to a dictionary
  162. var stones = DS.Dictionary<int>( new { Mick=1943, Keith=1943, Brian=1942, Bill=1936, Charlie=1941 } );
  163. var allTogether = beatles.Add(stones);
  164. var noStones = allTogether.Remove(stones);
  165. // Determine if a dictionary contains a key, a dictionary or literal dictionary
  166. var b1 = beatles.Include(beatles);
  167. var b2 = beatles.Include("Paul");
  168. var b3 = beatles.Include( new { Paul=1942, John=1940 } );
  169. // Process string template with the value of a dictionary
  170. var InstallVar = DS.Dictionary( new { SERVER="foo.bar.local", USER=@"bat\jshmoe", PASSWORD="abcd" } );
  171. var batchCode = InstallVar.PreProcess(@"msiexec.exe /i product.msi SERVER={SERVER} USER={USER} PASSWORD={PASSWORD}");
  172. if("Mick".In(stones)) {
  173. }
  174. }
  175. public static void MyFunction2(int i, string s, DateTime d) {
  176. var parameters = ReflectionHelper.GetLocals(i, s, d);
  177. var v1 = ReflectionHelper.GetLocals(i, s, d).Format(); // { i:1, s:"A", d:"4/21/2011 10:23:13 PM" }
  178. }
  179. public static void Sample14() {
  180. MyFunction2( 1, "A", DateTime.Now );
  181. }
  182. }
  183. }