Variable Scope For an Extension Method

Related searches

I have something interesting that I want to understand little deeper.

I have an extension class that extends int:

public static class MyExtension
    {
        public static void GetProp(this int i, MyObject o)
        {
            var val = i;
            o.sum = i;
        }
    }

That uses a class as one of its parameter:

public class MyObject
    {
        public int sum { get; set; }
    }

Now, lets see the unit test class:

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void test()
        {
            int a = 1;
            int b = 2;
            int sum = 0;
            Add(a, b, sum);

//Here, sum=3 but after its execution, sum looses its value and retains the value sum = 0

            int test = 4;
            MyObject obj = new MyObject();
            test.GetProp(obj);

But in the above code when I pass the variable using the extension method, the obj.sum retains its value obj.sum = 4 . I am not passing any value by reference. The first portion of code seems to follow the passing ByVal. But the second portion of extension method, the value is retained as if its passed as ByRef

            string s = sum.ToString();
            string p = obj.sum.ToString();
        }

        private void Add(int x, int y, int sum)
        {
            sum = x + y;
        }
    }

Can someone explain the mechanism behind this. Thanks

All parameters are send by value, unless you specify them with the ref or out keyword. Passing a parameter by value means that the value is copied.

However, when you pass an object by value it's not the object that is copied, it's the reference that is copied. That means that you have two references to the same object, one in the obj variable and one in the o parameter, but only one object.

When the extension method accessed the object, it's the same object as outside the method. Any change to the property made using the o parameter will be visible when you later access it using the obj variable.

Extension Methods, Extension methods are only in scope when you explicitly import the Extension methods cannot access private variables in the type they are� The method must be in scope when it is called. To call an extension method. Declare a variable that has the data type of the first parameter of the extension method. For PrintAndPunctuate, you need a String variable: Dim example = "Ready" That variable will invoke the extension method, and its value is bound to the first parameter, aString.

This is the difference between passing by reference and passing a reference object (class). In GetProp, you aren't modifying the reference obj, you are modifying the MyObject instance that is referred by obj.

Extension Methods in C#, Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive. Create a Class� The WordCount extension method can be brought into scope with this using directive: using ExtensionMethods; And it can be called from an application by using this syntax: string s = "Hello Extension Methods"; int i = s.WordCount(); You invoke the extension method in your code with instance method syntax.

If I understand your question, you're confused about why the sum variable isn't changed when it is passed by value, but the obj.sum property does retain its value when obj is passed by reference. The other answers do a good job of explaining this.

In your question, you pass a parameter by value as a receiver, and this confuses your question a bit. Your question appears to be, "why is it that when I pass normally, it's treated as by-value, but when I pass as a receiver to an extension method, it's by-reference?"

Ah. Try assigning a new value to the receiver and see what happens at the call site:

    public static void GetProp(this int i, MyObject o)
    {
        o.sum = i;
        i = 5000;
    }

You'll find that the variable test at the call site is not affected by this, since the receiver is also passed by value!

Tim Leffelman - C# Extension Methods, Extension methods have been part of the C# programming language keyword to indicate a variable number of arguments of the same type It is much more practical to use an extension method with a narrower scope. The part of the program where a particular variable is accessible is termed as the Scope of that variable. A variable can be defined in a class, method, loop etc. In C/C++, all identifiers are lexically (or statically) scoped, i.e.scope of a variable can be determined at compile time and independent of the function call stack.

Extensions, To declare an extension function, we need to prefix its name with a make new functions callable with the dot-notation on variables of this type. of other entities as regular functions declared in the same scope would. Variable scope. The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example:

Create better code using delegates and extension methods in Unity , Delegates and extension methods are tools in your C# toolbox that, method) is using variables or data from the outside scope, it won't be� Extension methods are useful for when you wish to define extensions on a generic type that will constrain the type variable. For example: namespace Extensions open System.Collections.Generic open System.Runtime.CompilerServices [<Extension>] type IEnumerableExtensions = [<Extension>] static member inline Sum(xs: IEnumerable<'T>) = Seq.sum xs

A Variables scope variable is available only on the page on which it is created and any included pages (see also the Caller scope).Variables scope variables created in a CFC are available only to the component and its functions, and not to the page that instantiates the component or calls its functions.

Comments
  • are you familiar with how to use the ref key word within an extension method stackoverflow.com/questions/2618597/…