C#新特性

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

namespace LocalApp.ConsoleApp
ExpandedBlockStart.gifContractedBlock.gif
{
    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
delegate void LambdaHandler();

        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//Func 封装一个具有 1 - 4 个参数并返回 TResult 参数指定的类型值的方法。

ExpandedSubBlockStart.gifContractedSubBlock.gif            Func
<intstring> func = delegate(int i) return Convert.ToString(i * i); };
            Console.WriteLine(func(
3));

ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//******************************************/

            
// lambda 表达式,i 参数
            Func<intstring> func2 = i => Convert.ToString(i * i);
            Console.WriteLine(func2(
4));

ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//******************************************/

            Func
<stringstring> func3 = a => a.ToUpper();

ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] array = "hebei","hubei","beijing","12" };

            IEnumerable
<string> _array = array.Where<string>(b => b.EndsWith("i")); // array.Select(func3);

            
foreach (string i in _array)
                Console.WriteLine(i);

ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//******************************************/

ExpandedSubBlockStart.gifContractedSubBlock.gif            Func
<stringstringstringstringstring> __func = (a, b, c, d) => return a + "_" + b + "_" + c + "_" + d; };

            Console.WriteLine(__func(
"h","e","l","lo"));

ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//******************************************/


            Func
<string> __func2 = () => "123456";

            Console.WriteLine(__func2());

ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//******************************************/

            LambdaHandler lam 
= () => Console.WriteLine( "1111111111");

            lam 
+= () => Console.WriteLine("22222222222");

            lam();

ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//******************************************/
            
            
//扩展方法
            string extTest = "hello world";

            Console.WriteLine(extTest.WordCount());

            
foreach(string i in extTest.WordSplit())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Console.WriteLine(i);
            }


ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//******************************************/
            Console.ReadKey(
true);
        }

    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 扩展方法
    
/// </summary>

    public static class Extensions
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public static int WordCount(this String str)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
return str.Split(new char[] ' ''.''?' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }


        
public static string[] WordSplit(this String str)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
return str.Split(new char[] {' ','.','?' }, StringSplitOptions.None);
        }

    }

}

没有更多推荐了,返回首页