开发者

What is the basic C# interface scope?

I am having a dispute with another fellow programmer, over the scope of interfaces.

suppose we have the following:

public interf开发者_运维技巧ace IFoo
{
    string Bar { get; set; }
}

public class SomeFoo: IFoo
{
    public string Bar { get; set; }

    public SomeFoo(string bar)
    {
        this.Bar = bar;
    }
}

public class Consumer
{
    public void DoSomething()
    {
        SomeFoo fooClassInstance = new SomeFoo("test");
        IFoo fooInterface = (IFoo)fooClassInstance;

        // do something with fooInterface.
    }
}

So the question is: 1. Is it possible the fooClassInstance to go out of scope before something else releases the fooInterface instance?

Some argue that the object (fooClassInstance) can go out of scope.

I believe that it cant. Sure, objects may or may not get disposed of by the GC when the GC decides that the object(s) are no longer in scope. However since Interfaces are by design an abstract contract whose members have to be implemented by the object that uses it, than an interface cannot lose its implementation so long as the interface is used. It's not like a whole another object is created of type "interface". The interface is merely a pointer to those abstract members of the implementor.

Can you guys help me resolve this dispute?

Thanks,

<bleepzter />


While there is definitely some confusion in the terms you are using (I'll let someone else tackle that issue), I think I understand what you are saying, and you are basically right.

In particular, it sounds like your coworker believes this is happening:

// Now there is this "SomeFoo" object somewhere in memory.
SomeFoo fooClassInstance = new SomeFoo("test");

// Now there is this "IFoo" object somewhere in memory.
IFoo fooInterface = (IFoo)fooClassInstance;

// Let's say down the road somewhere, fooClassInstance is set to null or a different
// object. Your coworker believes that the object it originally pointed to will then
// have no references to it and will thus be eligible for garbage collection?

If the above is an accurate representation of what your coworker thinks, then your coworker is wrong and you are right. The fooInterface variable contains a reference to the same object that fooClassInstance had a reference to. You can easily verify this simply by doing the following:

SomeFoo fooClassInstance = new SomeFoo("test");
IFoo fooInterface = (IFoo)fooClassInstance;
bool sameObject = ReferenceEquals(fooClassInstance, fooInterface);

If ReferenceEquals returns true, then the two variables are referencing the same object in memory.


If your coworker needs further convincing, try showing him/her something like this:

List<int> list = new List<int> { 1, 2, 3 };

// This cast is actually not needed; I'm just including it so that it mirrors
// your example code.
IList<int> ilist = (IList<int>)list;

// Now we remove an item from the List<int> object referenced by list.
list.Remove(3);

// Is it in ilist? No--they are the same List<int> object.
Console.WriteLine(ilist.Contains(3));

// How about we remove an item using ilist, now?
ilist.Remove(2);

// Is it in list? Nope--same object.
Console.WriteLine(list.Contains(2));

// And here's one last thing to note: the type of a VARIABLE is not the same
// as the type of the OBJECT it references. ilist may be typed as IList<int>,
// but it points to an object that is truly a List<int>.
Console.WriteLine(ilist.GetType());


I think you are muddling the line between instance an reference. fooClassInstance can go out of scope (that is, it can no longer be referenced) even if the instance of the object still exists (because fooInterface still holds a reference to it).

For example, if you do the following, fooClassInstance will not be available after the braces, but fooInterface will.

public void DoSomething()
{
    IFoo fooInterface;
    {
        SomeFoo fooClassInstance = new SomeFoo("test");
        fooInterface = (IFoo)fooClassInstance;
    } 

    // do something with fooInterface, but NOT with fooClassInstance
}


Uh, what do you mean by "lose its implementation"? That makes no sense. A type implements an interface, it cannot "unimplement" an interface.

As far as your code example goes, the lifetime of the object allocated ends the instant it no longer has any roots (aka, is no longer reachable by the GC). A reference to it is a reference to it, regardless of the type of the reference (i.e. if the reference type is of some derived or parent type, it doesn't matter).

ISomething Sample() {
    Something s1 = new Something();
    s2.DoSomething(); // Assuming s is the only reference to s, then it no longer is
                      // rooted after this expression

    Something s2 = new Something();
    ISomething is1 = s2;
    s2 = null;
    is1.DoSomething(); // The reference remains valid and the lifetime of the
                       // object created continues until we release all
                       // remaining references to it.
    return is1;
}


It sounds like you are correct, but I feel like we're missing part of the discussion since we don't know what you're doing with the object after you create (and cast) it.

In your example:

    SomeFoo fooClassInstance = new SomeFoo("test");

At this point, you have one reference to the SomeFoo object referenced by fooClassInstance.

    IFoo fooInterface = (IFoo)fooClassInstance;

At this point, you have two references to the SomeFoo object (referenced by both fooClassInstance and fooInterface).

So yes, depending on how you use it, fooClassInstance could go out of scope. But there is still a reference to it (fooInterface) so it wouldn't be garbage collected. Also, the fooInterface reference could be cast back to SomeFoo.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜