In-depth understanding of C# Chapter 11 Query Expressions for Method Calls

    class ShowAllUsersMethodCall
    {
        static void Main()
        {
            var query = SampleData.AllUsers.Select(user => user);
//query System.Linq.Enumerable.WhereSelectListIterator
            foreach (var user in query)//first foreach and then query
			//Following is in call user => user return in call varchar  
			//Call user => user once in is called, and the last time is to exit the loop
			//var user's value {User: Tim Trotter (Tester)} WriteLine after assignment
			// {User: Tara Tutu (Tester)}
			//{User: Deborah Denton (Developer)}
			//{User: Darren Dahlia (Developer)}
			//{User: Mary Malcop (Manager)}
			//{User: Colin Carton (Customer)}
			
            {
                Console.WriteLine(user);//Call the overridden ToString method
            }
        }
    }
	
    public class User
    {
        public string Name { get; set; }
        public UserType UserType { get; set; }

        public User (string name, UserType userType)
        {
            Name = name;
            UserType = userType;
        }

    public override string ToString()
    {
            return string.Format("User: {0} ({1})", Name, UserType);
    }
}	

	 public static IEnumerable<User> AllUsers
     {
         get { return users; }
     }
     public static class Users
     {
         public static readonly User TesterTim = new User("Tim Trotter", UserType.Tester);
         public static readonly User TesterTara = new User("Tara Tutu", UserType.Tester);
         public static readonly User DeveloperDeborah = new User("Deborah Denton", UserType.Developer);
         public static readonly User DeveloperDarren = new User("Darren Dahlia", UserType.Developer);
         public static readonly User ManagerMary = new User("Mary Malcop", UserType.Manager);
         public static readonly User CustomerColin = new User("Colin Carton", UserType.Customer);
     }
		
	public enum UserType : byte
    {
        Customer,
        Developer,
        Tester,
        Manager,
    }


Compiler translation is the translation based on query expressions. The
  compiler translates query expressions into ordinary C# code, which is the basis for supporting C#3 query expressions.
It does the conversion in a mechanical way, without understanding the code, applying type references, checking
the validity of method calls, or doing any normal work that the compiler is meant to do. These are performed
after .
  The C#3 compiler translates the query expression into such code before it further compiles it correctly.
In particular, it does not assume that Enumerable.Select should be used, or that List<T> should contain
a Select method call. It just transpiles the code and lets the next compilation phase handle
the work of finding the appropriate method -- whether that method is a directly included member, or an extension method. The parameter is an
appropriate delegate type or an Expression<T> corresponding to type T.

  Lambda expressions can be transformed into delegate instances and expression trees.


输出User: Tim Trotter (Tester)User: Tara Tutu (Tester)User: Deborah Denton (Developer)User: Darren Dahlia (Developer)User: Mary Malcop (Manager)User: Colin Carton (Customer)






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324686054&siteId=291194637