Everything new in C # 3.0 in one small program

    On the street the weather is not very good, I won’t go anywhere, I decided to create something perverted, I really wanted to ... I offer the code of a small program below. What should she give out to the console? Compile and test yourself ...

    For those who lower karma: do not like the post, go to other topics. He is on a personal blog. After all, I’m willing to comment, but I do not create virtuals, do not be so harsh ...



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Remoting.Messaging;

    namespace Frozik
    {
        static class Program
        {
            public static char FirstChar <TInput> ( this TInput value )
                where TInput: class
            {
                return value.ToString () [0];
            }

            static void Main ()
            {
                var arguments = new List < string > {Arg_0 , temp_z , arg_1 , arg_2 };

                object locker;

                lock (locker = new object ())
                {
                    Console.WriteLine ( "Started" );

                    (( Func < string >) (() =>
                    {
                        lock (locker)
                        {
                            var result = string .Empty;

                            foreach ( string arg in
                                (from argEntity inarguments
                                 where argEntity.StartsWith ( "arg_" )
                                 select argEntity.Substring (4)))
                            {
                                Console.WriteLine (result + = arg.FirstChar < string > ());
                            }

                            return result;
                        }
                    })). BeginInvoke (
                        ( delegate (IAsyncResult result) {
                            Console.WriteLine (
                                new
                                {
                                    Lenght =
                                        (( Func <string >) ((AsyncResult) result) .AsyncDelegate).
                                            EndInvoke (result) .Length
                                } .Lenght);
                        }),
                        new object ()
                    );

                    Console.WriteLine ( "Finished" );

                    Console.ReadKey ();
                }

                Console.ReadKey ();
            }
        }
    }

    Below are explanations for those who do not understand the syntax of


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Remoting.Messaging;

    namespace Frozik
    {
        static class Program
        {
            // Create an extension for all reference types, in my program the extension
            // will be applied only to strings. The extension I have is generic just to
            be more confusing.
            // How extensions work:
            // Methods can be added to any standard or non-standard type, as if they
            were // implemented in the class itself. Further in the program, I use the extension
            // for the string - arg.FirstChar <string> ()
            // Extensions must be public static and created in the
            public static char class FirstChar <TInput> (this TInput value)
                // The extension will be for reference types - generic types are available starting with C # 2.0 and .Net 2.0
                where TInput: class
            {
                return value.ToString () [0];
            }

            static void Main ()
            {
                // Innovation - for the place of calling the Add method, you can simply specify all the data as
                // when declaring the array. Also in this case, arguments will be of type List <string>,
                // intellisense will understand everything and will work correctly.
                var arguments = new List <string> {"arg_0", "temp_z", "arg_1", "arg_2"};

                object locker;

                lock (locker = new object ())
                {
                    Console.WriteLine ("Started");

                    // The lambda expression is essentially an anonymous delegate. For the standard type
                    // Func (function with a return parameter), you can also specify input parameters
                    // which are not needed in my example
                    // Anonymous delegate could be created for the lambda expression
                    // (delegate () {
                    ((Func <string> ) (() =>
                    // If there was a task, we could write the following:
                    // Console.WriteLine (((Func <int, int, string>) ((x, y) => (x + y). ToString ())) (5, 5));
                    // We would create a lambda expression that has 2 int parameters as input and
                    // string as the output , and call it with parameters 5 and 5. You can try this, it will output 10 to the console
                    // The example in the comment above is implemented with using an anonymous delegate -
                    // Console.WriteLine (((Func <int, int, string>) (delegate (int x, int y) {return (x + y) .ToString ();})) (5, 5) );
                    {
                        // Lambda expression has access to local variables that were
                        // defined in the parent method.
                        lock (locker)
                        {
                            // In this case, result we will have type string
                            var result = string.Empty;

                            foreach (string arg in
                                // Linq expression. The result will be IEnumerable <string>
                                // We look in arguments for all values ​​that begin with “arg_”
                                // and take only Substring (4) from each
                                result // found as the result . IEnumerable <string> is easy to turn into a List <string> or
                                // string [], just add .ToList () or ToArray () at the end
                                // Also this expression is similar to the following -
                                // arguments.Where ((argEntity) => argEntity.StartsWith ("arg_")).
                                // Select ((argEntity) => argEntity.Substring (4))
                                // You can see more details here - msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx
                                (from argEntity in arguments
                                 where argEntity.StartsWith ("arg_ »)
                                 Select argEntity.Substring (4)))
                            {
                                // Add the first letters of the lines and return the result of the addition
                                Console.WriteLine (result + = arg.FirstChar <string> ());
                            }

                            return result;
                        }
                    })).
                    // Since the lambda expression can be an anonymous delegate, then we can call it
                    // in a separate thread
                    BeginInvoke (
                        // Create an anonymous delegate that will be called when the request is executed,
                        // for this place we could write the lambda expression
                        / / (result) => {
                        (delegate (IAsyncResult result) {
                            Console.WriteLine (
                                // Create a type on the fly with one Length property,
                                // present here only to confuse
                                new
                                {
                                    Lenght = ((Func <string>) ((AsyncResult) result) .AsyncDelegate).
                                        EndInvoke (result) .Length
                                }.
                                // Get the Lenght property of the anonymous type
                                Lenght);
                        }),
                        new object ()
                    );

                    Console.WriteLine ("Finished");

                    Console.ReadKey ();
                }

                Console.ReadKey ();
            }
        }
    }

    // There were only locks left, but I won’t explain them, everyone should know this ;-)

    Also popular now: