Question

Even though all common sense says no, I still am asking this question just to get a second opinion and become sure.

If I have a class hierarchy like so:

public class IntermediateObjectContext : System.Data.Objects.ObjectContext
{
    public static Action<string> PrintHello { get; set; }
}

public class MyDatabaseContext : IntermediateObjectContext
{
    public ObjectSet<Foo> Foos
    {
      get { // ... }
    }
}

Then from a third, unrelated to Entity Framework object, if I access the static member of the IntermediateObjectContext class, in this case, if I subscribe to the delegate of the class, will that somehow instantiate a new ObjectContext?

class SomeClass
{
    public void SomeMethod()
    {
      IntermediateObjectContext.PrintHello += SayHello;
    }

    public void SayHello(string s)
    {
        Debug.Print(s);
    }
}

All reason says no, common sense says it won't, but I just want to make sure. I am trying to track down a memory hogger object.

What happens if

What happens to the memory situation if I have a static collection for SomeClass types like so:

public class SomeClassCollection
{
    private static Collection<SomeClass> _col = 
                new Collection<SomeClass>();

    public void Add(SomeClass c) { _col.Add(c); }

    public void Remove(SomeClass c) { _col.Remove(c); }
}

And then some code adds SomeClass instances to SomeClassCollection like so:

public SomeClassCollectionConfig
{
    public static RegisterSomeClasses()
    {
        SomeClassCollection.Add(new SomeClass());

        SomeClassCollection.Add(new DerivesClassOfSomeClass());
    }
}
Was it helpful?

Solution

(1) No, it won't instantiate an object.

(2) What happens if:

There it will allocate the empty collection col the first time any member of SomeClassCollection is accessed.

From the code, that's all it will do. You aren't using _col anywhere in the code presented.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top