views:

70

answers:

4

I was just trying to code the following extension method:

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

namespace _4Testing
{
    static class ExtensionMethods
    {
        public static void AssignMe(this int me, int value)
        {
            me = value;
        }
    }
}

But it is not working, i mean, can I use an extension method to alter values from extended classes? I don't want to change void return type to int, just changing extended class value. Thanks in advance

+1  A: 

Your example uses int, which is a value type. Classes are reference types and behaves a bit differently in this case.

While you could make a method that takes another reference like AssignMe(this MyClass me, MyClass other), the method would work on a copy of the reference, so if you assign other to me it would only affect the local copy of the reference.

Also, keep in mind that extension methods are just static methods in disguise. I.e. they can only access public members of the extended types.

public sealed class Foo {
   public int PublicValue;
   private int PrivateValue;
}

public static class FooExtensions {
   public static void Bar(this Foo f) {
      f.PublicValue = 42;

      // Doesn't compile as the extension method doesn't have access to Foo's internals
      f.PrivateValue = 42; 
   }
}
Brian Rasmussen
Cool Brian! Let me now go back to my IDE to continue changing my code.
Ramon Araujo
A: 

Please note that int is a value type. This means that it is copied by value. When your AssignMe method is called, a copy of the actual value is made and in that method you change that copy. The original value stays unchanged. This behavior has nothing to do with extension methods.

Steven
A: 

Ramon what you really need is a ref modifier on the first (i.e. int me ) parameter of the extension method, but C# does not allow ref modifier on parameters having 'this' modifiers.

[Update] No workaround should be possible for your particular case of an extension method for a value type. Here is the "reductio ad absurdum" that you are asking for if you are allowed to do what you want to do; consider the C# statement:
5.AssignMe(10);
... now what on earth do you think its suppose to do ? Are you trying to assign 10 to 5 ?? Operator overloading cannot help you either.

mumtaz
So, my question was looking for a workaround, do you please have one? Thanks anyway
Ramon Araujo
+1  A: 
// a work around for extension to a wrapping reference type is following ....

using System;


static class Program
{

    static void Main(string[] args)
   {


    var me = new Integer { value = 5 };
    int y = 2;

    me.AssignMe(y);

    Console.WriteLine(me); // prints 2

    Console.ReadLine();
    }

    public static void AssignMe(this Integer me, int value)
    {

       me.value = value;
    }


}

   class Integer
   {


      public int value { get; set; }

      public Integer()
   {
       value = 0;
   }

   public override string ToString()
   {
       return value.ToString();
   }

   }
mumtaz
Good mumtaz! I implemented a similar solution at the end.
Ramon Araujo