TestWhere

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

namespace ConsoleApplication1
{
    //在定义泛型类时,可以对客户端代码能够在实例化类时用于类型参数的类型种类施加限制。如果客户端代码尝试使用某个约束所不允许的类型来实例化类,则会产生编译时错误。这些限制称为约束。约束是使用 where 上下文关键字指定的。

    //  从 Visual C# 3.0 开始,在方法范围中声明的变量可以具有隐式类型 var。 隐式类型的本地变量是强类型变量(就好像您已经声明该类型一样),但由编译器确定类型。 下面的两个 i 声明在功能上是等效的:
    //复制 var i = 10; // implicitly typed
    //int i = 10; //explicitly typed

    class WhereSample
    {
        static void Main33()
        {
            // Simple data source. Arrays support IEnumerable<T>.
            int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
            // Simple query with one predicate in where clause.
            var queryLowNums = from num in numbers where num < 5 select num;
            foreach (var s in queryLowNums)
            {
                Console.Write(s.ToString() + " ");
            }
        }

        //在单一 where 子句内,可以使用 && 和 || 运算符根据需要指定任意多个谓词。
        static void Main5()
        {
            // Data source.
            int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
            // Create the query with two predicates in where clause.
            var queryLowNums2 = from num in numbers where num < 5 && num % 2 == 0 select num;
            // Execute the query
            foreach (var s in queryLowNums2)
            {
                Console.Write(s.ToString() + " ");
            }
        }

        static void Main56()
        {
            // Data source
            int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

            // Create the query with a method call in the where clause.
            // Note: This won't work in LINQ to SQL unless you have a stored procedure that is mapped to a method by this name.
            var queryEvenNums = from num in numbers where IsEven(num) select num;

            // Execute the query.
            foreach (var s in queryEvenNums)
            {
                Console.Write(s.ToString() + " ");
            }
        }

        // Method may be instance method or static method.
        static bool IsEven(int i)
        {
            return i % 2 == 0;
        }

    }
    //---------------------------------------------------
    public interface ISomeInterface
    { }
    public class BaseClass
    { }
    public class MyGenericClass<T> where T : BaseClass, ISomeInterface
    {
        //C# 编译器只允许将泛型参数隐式或强制转换到 Object 或约束指定的类型
        void SomeMethod(T t)
        {
            ISomeInterface obj1 = t;
            BaseClass obj2 = (BaseClass)t;
            object obj3 = t;
        }
    }

    // 编译器允许您将泛型参数显式强制转换到其他任何接口,但不能将其转换到类
    class SomeClass
    { }
    class MyClass1<T>
    {
        void SomeMethod(T t)
        {
            ISomeInterface obj1 = (ISomeInterface)t;  //Compiles  
            //SomeClass obj2 = (SomeClass)t;           //Does not compile  
        }
    }

    //使用临时的 Object 变量,将泛型参数强制转换到其他任何类型#region 使用临时的 Object 变量,将泛型参数强制转换到其他任何类型  
    class MyClass2<T>
    {
        void SomeMethod(T t)
        {
            object temp = t;
            SomeClass obj = (SomeClass)temp;
        }
    }

    //--------------------------------------------------------------------------------
    //除了接口约束,where 子句还可以包括基类约束,以指出某个类型必须将指定的类作为基类(或者就是该类本身),才能用作该泛型类型的类型参数。这样的约束一经使用,就必须出现在该类型参数的所有其他约束之前。

    class MyClass<T, U>
        where T : class
        where U : struct
    {

    }

    interface IMyInterface
    {

    }
    class Dictionary44<TKey, TVal>
        where TKey : IComparable, IEnumerable
        where TVal : IMyInterface
    {
        public void Add(TKey key, TVal val)
        {

        }

        //还可以将约束附加到泛型方法的类型参数
        public bool MyMethod<T>(T t) where T : IMyInterface
        {
            return false;
        }

    }


    //-----------------------------------------------------------
    public class Employee
    {
        private string name;
        private int id;

        public Employee(string s, int i)
        {
            name = s;
            id = i;
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int ID
        {
            get { return id; }
            set { id = value; }
        }
    }

    public class GenericList33<T> where T : Employee
    {
        private class Node
        {
            private Node next;
            private T data;

            public Node(T t)
            {
                next = null;
                data = t;
            }

            public Node Next
            {
                get { return next; }
                set { next = value; }
            }

            public T Data
            {
                get { return data; }
                set { data = value; }
            }
        }

        private Node head;

        public GenericList33() //constructor
        {
            head = null;
        }

        public void AddHead(T t)
        {
            Node n = new Node(t);
            n.Next = head;
            head = n;
        }

        public IEnumerator<T> GetEnumerator()
        {
            Node current = head;
            while (current != null)
            {
                yield return current.Data;
                current = current.Next;
            }
        }

        public T FindFirstOccurrence(string s)
        {
            Node current = head;
            T t = null;
            while (current != null)
            {
                //The constraint enables access to the Name property.
                if (current.Data.Name == s)
                {
                    t = current.Data;
                    break;
                }
                else
                {
                    current = current.Next;
                }
            }
            return t;
        }
    }
}


//------------------------------------
// public class DbObjectModel <T> : DbObjectModel <T, long> where T: DbObjectModel <T, long>

//1. public class DbObjectModel<T> 是类申明,T是要传进去的参数 Type类型
//2. 这个 DbObjectModel<T>类是继承DbObjectModel<T, long>类的
//3. where 限制了构造实例的条件, 就是 T:DbObjectModel <T, long>
//4. 条件 T:DbObjectModel <T, long> 意思就是1.里面传进去的参数类型必须是从DbObjectModel<T, long>继承的,若传的不是这种类型则会出错。


public class DbObjectModel<T, R>
{
    public T TObj = (T)Activator.CreateInstance<T>();
    public R RObj = (R)Activator.CreateInstance<R>();
}

public class DbObjectModel<T> : DbObjectModel<T, long> where T : DbObjectModel<int, long>
{

}

public class dddd
{
    public void dd()
    {
        List<DbObjectModel<DbObjectModel<int, long>>> M = new List<DbObjectModel<DbObjectModel<int, long>>>();
        DbObjectModel<DbObjectModel<int, long>> X = new DbObjectModel<DbObjectModel<int, long>>();
        X.TObj.TObj = 1; // int
        X.RObj = 2; // long
        M.Add(X);

        M.ForEach(delegate(DbObjectModel<DbObjectModel<int, long>> DM)
        {
            Console.Write(DM.TObj.TObj.ToString() + " " + DM.RObj.ToString());
        }

        );
    }
}

 

posted on 2011-11-15 22:35  breakpoint  阅读(109)  评论(0编辑  收藏  举报

导航