0

I know that you can serialize a delegate like such:

[Serializable]
public class Foo
{
    public Func<int,int> Del;
}
...
public static void WriteFoo(Foo foo)
{
    BinaryFormatter formatter = new BinaryFormatter();
    using (var stream = new FileStream("test.bin", FileMode.Create, FileAccess.Write, FileShare.None))
    {
        formatter.Serialize(stream, foo);
    }
}

But what will happen to objects that the function contains, for example, in this case:

int D = 10;
Func<int, int> del = x => x * x - D;
Foo f = new Foo();
f.Del = del;
WriteFoo(f);

What will happen to D? Will the value of D be preserved? What will happen if D is an object reference?

dbc
  • 104,963
  • 20
  • 228
  • 340
  • Side note: you may want to try it first yourself and than see if you need explanations... – Alexei Levenkov Feb 04 '16 at 01:45
  • Related or duplicate: http://stackoverflow.com/questions/1132702/could-we-save-delegates-in-a-file-c and http://stackoverflow.com/questions/1618530/serializing-a-list-of-anonymous-delegates – dbc Feb 04 '16 at 06:21

0 Answers0