private static void TestSimpleLinq() {
string[] list = new string[] { "1111", "2222", "3333" };
var p = from o in list select o;
foreach (var s in p)
Console.WriteLine(s);
}
|
private static void TestConditionLinq() {
string[] list = new string[] { "1111", "2222", "3333" };
var p = from o in list where o == "2222" select o;
foreach (var s in p)
Console.WriteLine(s);
}
|
var p = from o in list where o like "1%" select o;
|
var p = from o in list where o.Contains("2") select o;
|
private static void TestConditionLinq() {
IEnumerable<string> p = new string[] { "1111", "2222", "3333" }.Where<string>(delegate (string o) {</string></string>
return o == "2222";
});
foreach (string s in p) {
Console.WriteLine(s);
}
Console.ReadLine();
}
|
static void TestImplicitLocalVariable(){
var vint = 10;
var vstring = "TEST";
var vint64 = 9029349442;
var vdouble = 9.234;
Console.WriteLine("{0},{1},{2},{3}", vint.GetType().ToString(),
vstring.GetType().ToString(),
vint64.GetType().ToString(),
vdouble.GetType().ToString());
Console.ReadLine();
}
|
class Program{
private static var t = 15;
static void TestImplicitLocalVariable(var t) {}
}
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyExtensionMethod {
public static class TestExtensionMethod {
//extnsion methods must be defined in non generic static class.
public static int WordCount(this string v) {
return v.Length;
}
}
}
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyExtensionMethod;
namespace CSharpNew {
public static class TestExtensionConsumer {
public static void TestExtension() {
string s = "TEST";
Console.WriteLine(s.WordCount());
Console.ReadLine();
}
}
}
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FirstLinq {
public class ExtensionMethodAndGenerics {
private static void GenericTypeResovlerTest1() {
GenericTypeResolverTest v = new GenericTypeResolverTest();
v.Value = "TEST2";
v.Test();
}
}
//generic implicit type resolver.
public class GenericTypeResolverTest {
public string Value { get; set; }
public override string ToString() {
return Value.ToString();
}
}
public static class GenericTypeResolverMethodTest {
public static void Test<t>(this T obj)</t> {
Console.WriteLine(obj.ToString());
}
}
}
|
Test<string>()</string>
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FirstLinq{
public class ExtensionMethodAndGenerics {
private static void GenericTypeResovlerTest1() {
GenericTypeResolverTest[] v2 = new GenericTypeResolverTest[]{
new GenericTypeResolverTest(),
new GenericTypeResolverTest()};
v2.Test2();
}
}
//generic implicit type resolver.
public class GenericTypeResolverTest {
public string Value { get; set; }
public override string ToString() {
return Value.ToString();
}
}
public static class GenericTypeResolverMethodTest {
public static void Test2<t>(this IEnumerable<t> obj)</t></t> {
Console.WriteLine(obj.ToString());
}
}
}
|
void Test2<t> (IEnumerable<t> obj)</t></t>
void Test2<genericresolvertest>(IEnumerable<genericresolvertest> obj)</genericresolvertest></genericresolvertest>
|
IEnumerable<string> p = new string[] { "1111", "2222", "3333" }.Where<string>(delegate (string o) {</string></string>
return o == "2222";
});
|
var p = new string[] { "1111", "2222", "3333" }.Where<string>(<strong>l => l == "2222"</strong>);</string>
|
…………
namespace CSharpNew {
publicclass TestLamba {
public delegate int Sum(int x, int y);
publicvoid Test2() {
//lamba expression can be more complex.
Sum sFunc = (x, y) => {
var ret = x + y;
DateTime d = DateTime.Now;
Console.WriteLine("sum time is {0}",d.ToShortDateString());
return ret;
};
Console.WriteLine(sFunc(15, 20));
Console.ReadLine();
}
}
}
|
var p1 = new[]{new {Name = "code6421", Address = "Taipen", Title = "Manager"},
new {Name = "tom", Address = "Taipen", Title = "Manager"},
new {Name = "jeffray", Address = "NY", Title = "Programmer"}};
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FirstLinqHack {
class TestHacking {
public static void HackLinq() {
Persons<person> list = new Persons<person>();</person></person>
list.Add(new Person { Name = "Code6421", Age = 18, Address = "Taipen" });
var p1 = from o in list select o;
Console.WriteLine(p1[0].Name);
}
}
public sealed class Person {
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
}
public static class PersonsExtension {
public static Persons<tresult> Select<tsource tresult>(this Persons<tsource> source, </tsource></tsource></tresult>
Func<tsource tresult> selector)</tsource>
{
return null;
}
}
public class Persons<t> {</t>
private List<t> _list = new List<t>();</t></t>
public T this[int index] {
get
{
return _list[index];
}
}
public void Add(T item) {
_list.Add(item);
}
}
}
|
static void Main(string[] args) {
FirstLinqHack.TestHacking.HackLinq();
}
|
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1875300