开发者

What is new() function here?

I've been learning design patterns and I saw such a method call from a class :

class Client: SubjectAccessor {
    static void Main() {
        Console.WriteLine("Proxy Pattern\n");

        ISubject subject = new Proxy();
        Console.WriteLine(subject.开发者_如何学运维Requesy());

        subject = new(); //Here is what I am asking
        COnsole.WriteLine(subject.Request());
    }
}

As you can see there is a subject = new(); call there and I am wondering whether it is creating a new instance of Proxy or something else. I've not found anything related to this.

Your help is much appreciated.

If you need, I can paste the whole code or actually it is written on a book so I need to write it down here.

Thanks.


It is a typo in the book. There is no current version of C# in which that is valid (it should raise a "Type expected" compiler error). Without context it is impossible to know what it should be.


AFAIK it's wrong, and that code won't even compile.

The new keyword in C# can have only the 3 meanings described in this link:
http://msdn.microsoft.com/en-us/library/51y09td4%28v=VS.80%29.aspx


I was the technical editor of that book; I have a copy right in front of me now. My copy says:

class Client : SubjectAccessor { 
  static void Main() {
    Console.WriteLine("Proxy Pattern\n");

    ISubject subject = new Proxy();
    Console.WriteLine(subject.Request());
    Console.WriteLine(subject.Request());

    ProtectionProxy subject = new ProtectionProxy();
    Console.WriteLine(subject.Request());

Now, there is an error here; the variable "subject" has been declared twice. Apparently I did not catch the error when I reviewed the book. (The correct thing to do here is to remove the type from the second declaration of "subject").

However, that is not the error that you are reporting.

Are you sure that is not what your copy says? I have the December 2007 first edition; what edition do you have? Perhaps someone attempted to correct this error in a later edition and messed it up? My guess would be that someone tried to correct the error by removing both mentions of the ProtectionProxy type from the erroneous line rather than removing the first one.


That would be the proxy class. never seen such syntax before tho. Best not to use such things as it will only reduce readability..


I can't see how that compiles. "new" in this context is the new operator and this always expects a typename per C# syntax.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜