• c#3.0 特性


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Linq.Expressions;

    namespace NewFeature
    {
        class Person
        {
            public int ID;
            public int IDRole;
            public string FirstName;
            public string LastName;
        }

        static class Extend
        {
            /// <summary>
            /// String Extension Method,transform the white space to underscore.
            /// </summary>
            /// <param name="s"></param>
            /// <param name="isUpper"></param>
            /// <returns></returns>
            public static string SpaceToUnderscore(this string s, bool isUpper) //Extension Method,Notice [this] position
            {
                char[] charArray = s.ToCharArray();
                string result = null;
                foreach (char c in charArray)
                {
                    if (char.IsWhiteSpace(c))
                        result += "_";
                    else
                        result += c;
                }
                return isUpper ? result.ToUpper() : result;
            }


        }

        class Program
        {
            static void Main(string[] args)
            {
                CSharpNewFeatures();

                QueryAsMethod();

                Console.ReadKey();
            }

            static void CSharpNewFeatures()
            {
                //(1),Extension Method
                string str = "I'm jerry chou";
                Console.WriteLine("Extesion Method for string:\n{0}\n",str.SpaceToUnderscore(true));

                //(2),Lambda Expressions
                // delegate void Action(void);
                Action doAction = delegate() { Console.WriteLine("I'm nothing, just use Anonymous Method"); };
                Action doLambdaAtion = () => { Console.WriteLine("I'm anything, just use Lambda Expression\n"); };
                doAction();
                doLambdaAtion();

                //(3),Expression Tree
                Expression<Func<Person, bool>> e = p => p.ID == 1;
                BinaryExpression body = e.Body as BinaryExpression;
                MemberExpression left = body.Left as MemberExpression;
                ConstantExpression right = body.Right as ConstantExpression;
                Console.WriteLine("Expression Tree:\n{0}",left.ToString());
                Console.WriteLine(body.NodeType.ToString());
                Console.WriteLine(right.Value.ToString() + "\n");


                //(4),Anonymous Types,
                //(5),use Object Initializaion Expressions
                object obj = new { LastName = "Anderson", FirstName = "Brad" };
                object obj2 = new { LastName = "Jerry", Age = 26, IsMale = true };
                Console.WriteLine("Type:\t{0}\nToString:\t{1}", obj.GetType(), obj.ToString());
                Console.WriteLine("Type:\t{0}\nToString:\t{1}\n", obj2.GetType(), obj2.ToString());

                //(6),Implicitly Typed Local Variables
                var itlv = new { LastName = "Jerry", Age = 26, IsMale = true };
                Console.WriteLine("Implicitly Typed Loca Variables({0}):\nLast Name[{1}]\tAge[{2}]\tIsMale[{3}]\n",itlv.GetType(), itlv.LastName, itlv.Age, itlv.IsMale);
                
    //itlv.Age = 99; //Anonymous Type cannot be assigned to -- it is read only


            }
            /// <summary>
            /// LINQ中SQOs(Standard Query Operators),最终会在Compile-Time被编译器转换为函数调用
            /// </summary>
            static void QueryAsMethod()
            {
                string sentence = "the quick brown fox jumps over the lazy dog";
                // Split the string into individual words to create a collection.
                string[] words = sentence.Split(' ');

                // Using query expression syntax.
                var query = from word in words
                            group word.ToUpper() by word.Length into gr
                            orderby gr.Key
                            select new { Length = gr.Key, Words = gr };

                // Using method-based query syntax.
                var query2 = words.
                    GroupBy(w => w.Length, w => w.ToUpper()).
                    Select(g => new { Length = g.Key, Words = g }).
                    OrderBy(o => o.Length);

                foreach (var obj in query)
                {
                    Console.WriteLine("Words of length {0}:", obj.Length);
                    foreach (string word in obj.Words)
                        Console.WriteLine(word);
                }

    // This code example produces the following output:
                //
                // Words of length 3:
                // THE
                // FOX
                // THE
                // DOG
                // Words of length 4:
                // OVER
                // LAZY
                // Words of length 5:
                // QUICK
                // BROWN
                // JUMPS 
            }
        }
    }

    原文:http://blog.chinaunix.net/u/11680/showart_1414688.html
  • 相关阅读:
    rpm包方式安装oracle21c
    有限广播地址与直接广播地址
    忽略大小写查看文件夹下重复的文件方法
    No spring.config.import property has been defined 规格严格
    application.properties 转 yaml ( 在线) 规格严格
    PG快速构建海量数据方法 规格严格
    Postgresql 查看建表语句 命令 规格严格
    一文读懂PostgreSQL数据库透明数据加密 规格严格
    PG锁监控 规格严格
    Spring Cloud Config Client 规格严格
  • 原文地址:https://www.cnblogs.com/chenfulai/p/1389799.html
Copyright © 2020-2023  润新知