/DynamicSugar.Console/index.sample.cs
C# | 266 lines | 226 code | 35 blank | 5 comment | 3 complexity | fa6a58fc9220fe318c54b6d305931f1c MD5 | raw file
- using System;
- using System.Text;
- using System.Collections.Generic;
- using System.Linq;
- using DynamicSugar;
- using System.Dynamic;
- #if WINDOWS
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- #endif
- namespace N {
- public class samples {
- static void DoSomething(List<int> l){
- }
-
-
- static void Sample2(string [] args) {
- var i = 3;
- var Values = new List<int>() { 1, 2, 3 }; // Regular C#
- if(Values.Contains(i))
- Console.WriteLine("in the list");
- if(i.In(1,2,3)) // Dynamic Sugar Syntax
- Console.WriteLine("in the list");
- }
- void MyFunction(IDictionary<string, object> d){
- }
- void Sample3(){
- MyFunction(new Dictionary<string,object>() { // Regular C#
- { "Debug" , true },
- { "Continent", "North America" }
- });
- // Dynamic Sugar Syntax
- MyFunction( DS.Dictionary( new { Debug=true, Continent="North America" } ) );
-
- // In() method
- var stones = DS.Dictionary<int>( new { Mick=1943, Keith=1943, Brian=1942, Bill=1936, Charlie=1941 } );
- if("Mick".In(stones)) {
- }
- }
- void MyFunction( IDictionary<string, bool> d){
- }
- void Sample4(){
- MyFunction(new Dictionary<string, bool>() { // Regular C#
- { "Debug" , true },
- { "Verbose", false }
- });
- // Dynamic Sugar Syntax
- MyFunction( DS.Dictionary<bool>( new { Debug=true, Verbose=false } ) );
- }
- // Returning anonymous types from a method and accessing members without reflection.
- void Sample5(){
- var firstName = "Fred";
- var age = 45;
-
- var s1 = String.Format("firstName:{0}, age:{1}", firstName, age ); // Regular C#
- // Dynamic Sugar Syntax. Is that beautiful?
- var s2 = "firstName:{firstName}, age:{age}".Template(new { firstName, age });
- }
-
- class Person {
- public string Name { set; get; }
- public int Age { set; get; }
- public string Format(string format, params object[] args) {
- return DynamicSugar.ExtendedFormat.Format(this, format, args);
- }
- }
- void Sample6(){
- var p = new Person() { Name = "Fred", Age = 45 };
- Console.WriteLine( String.Format( "Name={0}, Age={1:000}", p.Name, p.Age ) ); // Regular C#
-
- Console.WriteLine( p.Format( "Name={Name}, Age={Age:000}" ) ); // Dynamic Sugar Syntax
- }
-
- public static void Sample6_2(){
-
- var v1 = DS.List(1, 2, 3).Format(); // 1, 2, 3
- var v2 = DS.List(1, 2, 3).Format( @"""{0}""", "; " ); // "1"; "2"; "3"
- var v3 = DS.List("a", "b", "c").Format(); // "a", "b", "c"
- var v4 = DS.List("a", "b", "c").Format( @"({0})", "; " ); // ("a"); ("b"); ("c")
- var beatles = DS.Dictionary<int>( new { Paul=1942, John=1940, Richard=1940, George=1943 } );
- var v5 = beatles.Format(); // { Paul:1942, John:1940, Richard:1940, George:1943 }
- var v6 = beatles.Format(@"""{0}""=""{1}""", "; "); // { "Paul"="1942"; "John"="1940"; "Richard"="1940"; "George"="1943" }
- }
- // Regular C#
- static bool ComputeValues(int value, out double amount, out string message){
- amount = 2.0 * value;
- message = "Hello";
- return true;
- }
- // Dynamic Sugar Syntax
- static dynamic ComputeValues(int value){
- return DS.Values( new { returnValue = true, amount = 2.0 * value, message = "Hello" } );
- }
- void Sample7(){
- // Regular C#
- string message;
- double amount;
- var returnStatus = ComputeValues( 2, out amount, out message );
- Console.WriteLine( "returnStatus:{0}, amount:{1}, message:{2}", returnStatus, amount, message );
- // Dynamic Sugar Syntax
- var r = ComputeValues( 2 );
- Console.WriteLine("returnStatus:{0}, amount:{1}, message:{2}", r.returnValue, r.amount, r.message );
- }
- void Sample8(){
- for(int i=0; i<10; i++) // Regular C#
- Console.WriteLine(i);
- foreach(var i in DS.Range(10)) // Dynamic Sugar Syntax
- Console.WriteLine(i);
- }
- public static void Sample9(){
-
- var v1 = DS.List(1, 2, 3).Add( DS.List( 4, 5, 6 ) );
- var v2 = DS.List(1, 2, 3).Clone();
- var v3 = DS.List(1, 2, 3).Filter( e => e % 2 == 0 ); // Same as FindAll() for IEnumerable<>
- var v5 = DS.List(1, 2, 3).Identical( DS.List( 1, 2, 3 ) );
- var v6 = DS.List(1, 2, 3).Include( DS.List( 2, 3 ) );
- var v7 = DS.List(1, 2, 3).Include( 2, 3 );
- var v8 = DS.List(1, 2, 3).Intersect( DS.List( 3, 4, 5) ); // Same as Intersect() for IEnumerable<>
- var v9 = DS.List(1, 2, 3).IsNullOrEmpty();
- var v10 = DS.List(1, 2, 3).Map( e => e * 2 ); // Same as Select() for IEnumerable<>
- var v11 = DS.List(1, 2, 3).Merge( DS.List(3, 4, 5) );
- var v12 = DS.List(1, 2, 3).Reject( e => e % 2 == 0 );
- var v13 = DS.List(1, 2, 3).Substract( DS.List( 3, 4, 5) );
- DS.List(1, 2, 3).ToFile(@"C:\MyFile.txt");
- var v15 = DS.ListHelper.FromFile<int>(@"C:\MyFile.txt");
- var v16 = DS.List(1, 2, 3).Without( DS.List(2, 3) );
- var v17 = DS.List(1, 2, 3).Without( 2, 3 );
- }
-
-
- void Sample10(){
- var v1 = DS.List(1, 2, 3);
- while(!v1.IsEmpty()) {
- Console.WriteLine("{0} - {1}", v1.First(), v1.Last());
- v1 = v1.Rest();
- }
- }
- public void Sample11() {
- var s = "ABCD";
- #if WINDOWS
- Assert.AreEqual("ABCD" , s.Slice(0 ));
- Assert.AreEqual("BCD" , s.Slice(1 ));
- Assert.AreEqual("AB" , s.Slice(0 , 2));
- Assert.AreEqual("BC" , s.Slice(1 , 2));
- Assert.AreEqual("DCBA" , s.Slice(-1 ));
- Assert.AreEqual("CB" , s.Slice(-2, 2));
- #endif
- }
- public static void Sample12() {
- #if WINDOWS
- Assert.AreEqual("DCBA", "ABCD".Reverse());
-
- Assert.AreEqual("1;2;3", ";".Join(DS.List(1, 2, 3)));
-
- string s1 = null;
- Assert.AreEqual("default", s1.IfNullOrEmpty("default"));
- if(s1.IsNullOrEmpty()){
- }
- Assert.AreEqual("Lastname", "lastname".Capitalize());
- Assert.AreEqual("1,2,3,", ",1,2,3,".RemoveFirstChar());
- Assert.AreEqual("1,2,3,", ",1,2,3,".RemoveFirstChar(','));
- Assert.AreEqual(",1,2,3", ",1,2,3,".RemoveLastChar());
- Assert.AreEqual(",1,2,3", ",1,2,3,".RemoveLastChar(','));
- Assert.AreEqual("1,2,3", ",1,2,3,".RemoveLastChar().RemoveFirstChar());
- #endif
- }
- public static void Sample13() {
- var beatles = DS.Dictionary<int>( new { Paul=1942, John=1940, Richard=1940, George=1943 } );
- var other = beatles.Get("Pete", 1941); // Support a default value for non existing key
-
- var younger1 = beatles.Max(); // Return the entry with the greatest value
- var younger2 = beatles.Max(DS.List("Paul", "John", "Richard"));
- var older1 = beatles.Min(); // Return the entry with the smallest value
- var older2 = beatles.Min(DS.List("Paul", "Richard", "George"));
- var newMusicians = beatles.Clone();
- // Add or remove entries to a dictionary
- var stones = DS.Dictionary<int>( new { Mick=1943, Keith=1943, Brian=1942, Bill=1936, Charlie=1941 } );
- var allTogether = beatles.Add(stones);
- var noStones = allTogether.Remove(stones);
- // Determine if a dictionary contains a key, a dictionary or literal dictionary
- var b1 = beatles.Include(beatles);
- var b2 = beatles.Include("Paul");
- var b3 = beatles.Include( new { Paul=1942, John=1940 } );
- // Process string template with the value of a dictionary
- var InstallVar = DS.Dictionary( new { SERVER="foo.bar.local", USER=@"bat\jshmoe", PASSWORD="abcd" } );
- var batchCode = InstallVar.PreProcess(@"msiexec.exe /i product.msi SERVER={SERVER} USER={USER} PASSWORD={PASSWORD}");
- if("Mick".In(stones)) {
- }
- }
- public static void MyFunction2(int i, string s, DateTime d) {
- var parameters = ReflectionHelper.GetLocals(i, s, d);
- var v1 = ReflectionHelper.GetLocals(i, s, d).Format(); // { i:1, s:"A", d:"4/21/2011 10:23:13 PM" }
- }
- public static void Sample14() {
- MyFunction2( 1, "A", DateTime.Now );
- }
- }
- }