Behind the scenes of thewww.weide1946.com C# yield keyword(转

2019-05-03 22:26 来源:未知

1.Optional and Named Parameters 

 

  calls these methods can optionally not specify some of the arguments, thereby accepting the default values.

Behind the scenes of the C# yield keyword

June 9, 2008 by Lars Corneliussen

After reading the great article about the code-saving yield keyword “Give way to the yield keyword” by Shay Friedman I thought it could be interesting to know how the yield keyword works behind the scenes.

…it doesn’t really end the method’s execution. yield return pauses the method execution and the next time you call it (for the next enumeration value), the method will continue to execute from the last yield return call. It sounds a bit confusing I think… (ShayF)

By using yield return within a method that returns IEnumerable or IEnumeratorthe language feature is activated.

Note: IEnumerable is kind of a stateless factory for Enumerators.IEnumerable.GetEnumerator() is thread safe and can be called multiple times, while the returned stateful Enumerator is just a helper for enumerating contained values once. By contract IEnumerator offers a Reset() method, but many implementations just throw a NotSupportedException.

Lets create an enumerator method that yields some Fibonacci nubmers.

public class YieldingClass
{
    public IEnumerable<int> GetFibonachiSequence()
    {
        yield return 1;
        yield return 2;
        yield return 3;
        yield return 5;
    }
}

 

Note: Yield is not a feature of the .Net runtime. It is just a C# language feature which gets compiled into simple IL code by the C# compiler.

The compiler now generates a inner class with following signature (Reflector  some renaming):

[CompilerGenerated]
private sealed class YieldingEnumerator :
   IEnumerable<object>, IEnumerator<object>
{
    // Fields
    private int state;
    private int current;
    public YieldingClass owner;
    private int initialThreadId;

    // Methods
    [DebuggerHidden]
    public YieldingEnumerator(int state);
    private bool MoveNext();
    [DebuggerHidden]
    IEnumerator<int> IEnumerable<int>.GetEnumerator();
    [DebuggerHidden]
    IEnumerator IEnumerable.GetEnumerator();
    [DebuggerHidden]
    void IEnumerator.Reset();
    void IDisposable.Dispose();

    // Properties
    object IEnumerator<object>.Current
    { [DebuggerHidden] get; }

    object IEnumerator.Current
    { [DebuggerHidden] get; }
}

 

The original method GetFibonachiSequence() only returns a new instance of the YieldingEnumerator, passing the initial state –2 as well as itself as the owner.

Each enumerator holds a state indicating:

  • -2: Initialized as Enumerable. (Not yet an Enumerator)
  • -1: Closed
  • 0: Initialized as Enumerator.  
    If a new Enumerator is requested on the same instance, GetEnumerator() returns another new instance of YieldingEnumerator.
  •  1-n: Index of the yield return in the original GetFibonachiSequence()method. In case of nested enumerators or other more complex scenarios one yield return consumes more than one index.

The content of GetFibonachiSequence() is translated into YieldingEnumerator.MoveNext().

In our very simple scenario the code looks like this:

 

bool MoveNext()
{
    switch (state)
    {
        case 0:
            state = -1;
            current = 1;
            state = 1;
            return true;

        case 1:
            state = -1;
            current = 2;
            state = 2;
            return true;

        case 2:
            state = -1;
            current = 3;
            state = 3;
            return true;

        case 3:
            state = -1;
            current = 5;
            state = 4;
            return true;

        case 4:
            state = -1;
            break;
    }
    return false;
}

 

Quite easy, isn’t it?

So far we easily could have created the classes and methods used to enable the yield keyword ourselves, too.

But in more complex scenarios Microsoft does some tricks, which won’t compile as C# – at least not how Reflector translates the resulting IL code.

Lets have a look at some code with a nested enumeration…

foreach(int i in new int[] {1, 2, 3, 5, 8})
{
    yield return i;
}

 

This compiles into:

private bool MoveNext()
{
    try
    {
        switch (state)
        {
            case 0:
                state = -1;
                state = 1;
                this.values = new int[] { 1, 2, 3, 5, 8 };
                this.currentPositionInValues = 0;
                while (this.currentPositionInValues < this.values.Length)
                {
                    current_i = this.values[this.currentPositionInValues];
                    current = current_i;
                    state = 2;
                    return true;
                Label_007F:
                    state = 1;
                    this.currentPositionInValues  ;
                }
                this.Finally2();
                break;

            case 2:
                goto Label_007F;
        }
        return false;
    }
    fault
    {
        this.System.IDisposable.Dispose();
    }
}
[/sourcecode]

 

Now the states 1 and 2 are used to indicate whether the enumerator actually is at some point (2), or wether it is trying to retrieve the next value (1).

Two things would not compile:

  • goto Label_007F is used to jump back into the iteration over int[] values. The C# goto statement is not able to jump into another statements context. But in IL this is totally valid as a while statement in MSIL is nothing but some gotos either.
  • The fault is proper MSIL, but not supported in C#. Basically it acts as a finally which just is executed in case of an error.

Attention: As in anonymous delegates, parameters as well as local and instance variables are passed to the YieldingEnumerator only once. Read this great post on this: Variable Scoping in Anonymous Delegates in C#

 

 Thanks for your attention!

  when you call a method, you can specify arguments by using the name of their parameters.

  When you pass arguments to a method, the compiler evaluates the arguments from left to right.  

示例:

  www.weide1946.com 1  

  www.weide1946.com 2

  www.weide1946.com 3

 

Rules and Guidelines

when defining a method that specifies default values for some of its parameters:

  1.can specify default values for the parameters of methods, constructor methods, parameterful properties (C# indexers)

  can specify default values for parameters that are part of a delegate definition. Then, when invoking a variable of this delegate type, you can omit the arguments and accept the default values.

  2.Parameters with default values must come after any parameters that do not have default values.That is, after you define a parameter as having a default value, then all parameters to the right of it must also have default values.  

  a params array parameter (discussed later in this chapter) must come after all parameters (including those that have default values), and the array cannot have a default value itself.

**  For example, in the definition of my M method, I would get a compiler error if I removed the default value ("A") for s.**

**  3.**Default values must be constant values known at compile time.

  This means that you can set default values for parameters of types that C# considers to be primitive types. This also includes enumerated types, and any reference type can be set to null.

  For a parameter of an arbitrary value type,you can set the default value to be an instance of the value type, with all its fields containing zeroes.You can use the default keyword or the new keyword to express this; both syntaxes produce identical Intermediate Language (IL) code. 

  Examples of both syntaxes are used by my M method for setting the default value for the dt parameter and guid parameter, respectively.

   4.Be careful not to rename parameter variables because any callers who are passing arguments by parameter name will have to modify their code.

  For example, in the declaration of my M method, if I rename the dt variable to dateTime, then my third call to M in the earlier code will cause the compiler to produce the following message: error CS1739: The best overload for 'M' does not have a parameter named 'dt'.

   5.Be aware that changing a parameter’s default value is potentially dangerous if the method is called from outside the module.

  A call site embeds the default value into its call. If you later change the parameter’s default value and do not recompile the code containing the call site,then it will call your method passing the old default value.

  You might want to consider using a default value of 0/null as a sentinel to indicate default behavior;this allows you to change your default without having to recompile all the code with call sites.

**  www.weide1946.com 4**

  6.cannot set default values for parameters marked with either the ref or out keywords

  because there is no way to pass a meaningful default value for these parameters.

 

when calling a method by using optional or named parameters:

  1.Arguments can be passed in any order;

  however, named arguments must always appear at the end of the argument list.

  2.You can pass arguments by name to parameters that do not have default values,

  but all required arguments must be passed (by position or by name) for the compiler to compile the code

  3.C# doesn’t allow you to omit arguments between commas, as in M(1, ,DateTime.Now),because this could lead to unreadable comma-counting code.

  Pass arguments by way of their parameter name if you want to omit some arguments for parameters with default values.

  4.To pass an argument by parameter name that requires ref/out

  www.weide1946.com 5

 

 

 

C#’s optional and named parameter features are really convenient when writing C# code that interoperates with the COM object model in Microsoft Office. And, when calling a COM component, C# also allows you to omit ref/out when passing an argument by reference to simplify the coding even more. When not calling a COM component, C# requires that the out/ref keyword be applied to the argument.

 

The DefaultParameterValue and Optional Attributes

question:want programmers to define a method indicating which parameters are optional ,and what their default value should be in a programming language and then give programmers working in other programming languages the ability to call them.

answer:the compiler of choice must allow the caller to omit some arguments and have a way of determining what those arguments’ default values should be.

 

how?

  In C#, when you give a parameter a default value, the compiler internally applies the System.Runtime.InteropServices.OptionalAttribute custom attribute to the parameter, and this attribute is persisted in the resulting file’s metadata.

  In addition, the compiler applies System.Runtime.InteropServices.DefaultParameterValueAttribute to the parameter and persists this attribute in the resulting file’s metadata.

  Then, DefaultParameterValueAttribute’s constructor is passed the constant value that you specified in your source code.

**  **when a compiler sees that you have code calling a method that is missing some arguments,the compiler can ensure that you’ve omitted optional arguments, grab their default values out of metadata, and embed the values in the call for you automatically.

2.Implicitly Typed Local Variables

C# supports the ability to infer the type of a method’s local variable from the type of expression that is used to initialize it.

示例:

  www.weide1946.com 6

  using the C# var token. To determine the type of the name variable, the compiler looks at the type of the expression on the right side of the assignment operator (=).

  error CS0815: Cannot assign <null> to an implicitlytyped local variable) because null is implicitly castable to any reference type or nullable value *type; therefore, the compiler cannot infer a distinct type for it.***

*  it is possible to initialize an implicitly typed local variable with null if you explicitly specify a type (String, in my example). Although this is possible, it is not that useful because you could also **write String x = null; to get the same result.***

  In the fourth assignment,Not only is this a lot of typing, but if you ever decide to change the collection type or any of the generic parameter types, then you would not have to modify your code on both sides of the assignment operator, too.

*  use var to have the compiler automatically infer the type of the elements. This demonstrates that it is possible and quite useful to use var with *foreach, using, and for statements. It can also be useful when experimenting with code.**

 

C#’s implicitly typed local variable feature must be used when working with anonymous types within a method

不可能应用的情景:

  1.You cannot declare a method’s parameter type by using var.

*  The reason for this should be obvious to you because the compiler would have to infer the parameter’s type from the argument being **passed at a callsite and there could be no call sites or many call sites***

*  2.you cannot declare a *type’s field by using var. There are many reasons why C# has this restriction.**

  One reason is that fields *can be accessed by several methods and the C# team feels that this contract (the type of the variable) *should be stated explicitly.**

  Another reason is that allowing this would permit an anonymous type ***(discussed in Chapter 10) to leak outside of a single method.*

 

var comprare with dynamic:

  Do not confuse dynamic and var.

*  Declaring a local variable by using var is just *a syntactical shortcut that has the compiler infer the specific data type from an expression.**

  **The var keyword can be used only for declaring local variables inside a method, whereas **the dynamic keyword can be used for local variables, fields, and arguments.**

  You cannot ***cast an expression to var, but you can cast an expression to dynamic.*

*  You must explicitly *initialize a variable declared using var, whereas you do not have to initialize a variable declared ***with dynamic.***

3.Passing Parameters by Reference to a Method

1.By default, the common language runtime (CLR) assumes that all method parameters are passed by value.**

  When reference type objects are passed, the reference (or pointer) to the object is passed (by *value) to the method.*This means that the method can modify the object and the caller will see the *change.*

*  For value type instances, a copy of the instance is passed to the method. This means that the **method gets its own private copy of the value type and the instance in the caller isn’t affected.*

  so,In a method, you must know whether each parameter passed is a reference type or a value type because the code you write to manipulate the parameter could be markedly different.

*2.The CLR allows you to pass parameters by reference instead of by value.*

*  In C#, you do this by using the out and ref keywords. Both keywords tell the C# compiler to emit metadata indicating that this **designated parameter is passed by reference***

  the compiler uses this to generate code to pass the **address of the parameter rather than the parameter itself.**

 

ref and out:

  1.From the CLR’s perspective, out and ref are identical—that is, the same IL is produced regardless of which keyword you use, and the metadata is also identical except for 1 bit, which is used to record whether you specified out or ref when declaring the method.

  2.However, the C# compiler **treats the two keywords differently, and the difference has to do with which method is responsible for ***initializing the object being referred to.*

*  If a method’s parameter is marked with out, the caller isn’t *expected to have initialized the object prior to calling the method. The called method can’t read from ***the value, and the called method must write to the value before returning.***

***  If a method’s parameter *is marked with ref, the caller must initialize the parameter’s value prior to calling the method. The ***called method can read from the value and/or write to the value.*

*  **To summarize, from an IL or a CLR perspective, out and ref do exactly the same thing: they both cause a pointer to the instance to be passed. The difference is that the compiler helps ensure that **your code is correct.***

*  3.Using out and ref with value types gives you the same behavior that you already get when passing *reference types by value.**

  With reference types, the caller allocates memory for a pointer to a reference object,and the callee manipulates this pointer. Because of this behavior, using out and ref with reference *types is useful only when the method is going to “return” a reference to an object that it knows *about.**

*  With value types, out and ref allow a method to manipulate a single value type instance. The caller must allocate the memory for the instance, and the callee manipulates **that memory.***

*  4.**Using out with large value types is efficient because it prevents instances of the value type’s *fields from being copied when making method calls.**

  5.the CLR allows you to overload methods based on their use of out and ref parameters.

  www.weide1946.com 7

  It’s not legal to overload methods that differ only by out and ref because the metadata representation of the method’s signature for the methods would be identical.So I couldn’t also define the following method in the preceding Point type.**

  static void Add(out Point p) { ... }

   error CS0663: 'Add' cannot define overloaded methods because it differs only on ref and out.**

 

 

why C# requires that a call to a method must specify out or ref?

 

*  After all, the compiler knows whether the method being called requires out or ref and should be able to compile the code correctly. It turns out that the compiler can indeed do the right thing automatically.*

 

*  However, the designers of the C# language felt that the caller should explicitly state its intention. This way, at the call site, it’s obvious that the method being called is expected to change the value of the variable being passed.*

 

 

示例1:ref、out的区别

  www.weide1946.com 8

  The address of x is then passed to GetVal. GetVal’s v is a pointer to the Int32 value in Main’s stack frame. Inside GetVal, the Int32 that v points to is changed to 10

  www.weide1946.com 9

  Inside AddVal,the Int32 that v points to is required to have a value already. So, AddVal can use the initial value in any expression it desires. AddVal can also change the value, and the new value will be “returned” to **the caller.**

*  attempts to pass an uninitialized value to a method expecting a ref parameter produces the following message: error CS0165: Use of unassigned **local variable 'x'.***

示范二:引用类型优化

  www.weide1946.com 10www.weide1946.com 11

示例3:值互换

  www.weide1946.com 12

  www.weide1946.com 13www.weide1946.com 14

  The problem is that variables passed by reference to a method must be of the same type as declared in the method signature. In other words, Swap expects two Object references, not two String references.

  优化措施二

  www.weide1946.com 15

  www.weide1946.com 16

  For some other examples that use generics to solve this problem, see System.Threading’s Interlocked class with its CompareExchange and Exchange methods. 

以身作则四:传参相配难题

  www.weide1946.com 17

  won’t compile,because the parameters passed must match the parameters expected by the method is to ensure that type safety is preserved.

 

4.Passing a Variable Number of Arguments to a Method

convenient

 

how to achieve?

*  the params keyword  tells the compiler to apply an instance of the System.ParamArrayAttribute custom attribute to *the parameter.**

*  www.weide1946.com 18www.weide1946.com 19效能同样www.weide1946.com 20*

  no Add method is defined that takes five Int32-compatible arguments;

*  however, the compiler sees that the source code has a call to Add that is being passed a list of Int32 **values and that there is an Add method whose array-of-Int32 parameter is marked with the ParamArray *attribute.**

  So the compiler considers this a match and generates code that coerces the parameters ***into an Int32 array and then calls the Add method.*

*  The end result is that you can write the *code, easily passing a bunch of parameters to Add, but the compiler generates code as though you’d ***written the first version that explicitly constructs and initializes the array.***

 

compiler?

  When the C# compiler detects a call to a method.

*  the compiler checks all of the methods with the specified name, where no parameter has the ParamArray attribute applied. If a method exists that **can accept the call, the compiler generates the code necessary to call the method.***

   However, if the **compiler can’t find a match, it looks for methods that have a ParamArray attribute to see whether ***the call can be satisfied.*

*   If the compiler finds a match, it emits code that constructs an array and *populates its elements before emitting the code that calls the selected method.**

 

**limites?**

**  1.Only the last parameter to a method can be marked with the params keyword (ParamArrayAttribute).**

**  2.This parameter must also identify a single-dimension array of any type.**

**  3.*It’s legal to *pass null or a reference to an array of 0 entries as the last parameter to the method.**

  4.you can write a method that takes an arbitrary number of parameters where the parameters could be any type.

  5.Be aware that calling a method that takes a variable number of arguments incurs an additional performance hit unless you explicitly pass null.To help reduce the performance hit associated with this, you may want to consider defining a few overloaded methods that do not *use the params keyword* **

*  After all, an array object must be allocated on the heap, the array’s elements must be initialized, and the array’s **memory must ultimately be garbage collected.***

 

  www.weide1946.com 21www.weide1946.com 22www.weide1946.com 23

  the Concat method defines several overloads that do not use the params keyword. These versions of the Concat method are the most frequently called overloads, and these overloads exist in order to improve performance for the most common scenarios. **The overloads that use the params keyword are there for the less common scenarios; ***these scenarios will suffer a performance hit, but fortunately, they are rare.*

5.Parameter and Return Type Guidelines

1.When declaring a method’s parameter types, you should specify the weakest type possible, preferring interfaces over base classes.**

*because it is much more flexible and can be used ***in a much wider range of scenarios.**

www.weide1946.com 24www.weide1946.com 25

2.declare a method’s return type by using the strongest type possible (trying not to commit yourself to a specific type).**

 www.weide1946.com 26www.weide1946.com 27

but,when you want let the caller have as much flexibility as possible when calling a method, allowing the method to be used *in the widest range of scenarios.in which you want to leave yourself some flexibility *to change what your method returns, choose a weaker return type.**

Notice in this example that I’m using the strongest of the weakest types. For instance, I’m not using an IEnumerable<String> or even ICollection<String>.

6.Const-ness

why the CLR does not support constant objects/arguments?

  In some languages, such as unmanaged C , it is possible to declare methods or parameters as a constantthat forbids the code in an instance method from changing any of the object’s fields or preventsthe code from modifying any of the objects passed into the method.

  1.in unmanaged C , marking an instance method or parameter as const ensured only that the programmer could not write normal code that would modify the object or parameter.

  Inside the method, it was always possible to write code that could mutate the object/**parameter by either casting away the const-ness or by getting the address of the object/argument ***and then writing to the address*

   In a sense, unmanaged C lied to programmers, making them believe that their constant objects/arguments couldn’t be written to even though they could.**

  2.When designing a type’s implementation, the developer can just avoid writing code that manipulates the object/arguments.

  For example, strings are immutable because the String class doesn’t offer any methods that can change a string object.

    3.it would be very difficult for Microsoft to endow the CLR with the ability to verify that a constant object/argument isn’t being mutated.**

  The CLR would have to verify at each write that the write was not occurring to a constant object, and this would hurt performance significantly

  4.constant support adds a lot of complexity for developers.

  For example, if a type is immutable, all derived types would *have to respect this.***

  In addition, an immutable type would probably have to consist of fields that are **also of immutable types.**

 

版权声明:本文由韦德娱乐1946_韦德娱乐1946网页版|韦德国际1946官网发布于网络编程,转载请注明出处:Behind the scenes of thewww.weide1946.com C# yield keyword(转