What is the new () function?

I study design patterns, and I saw a method call from a class like this:

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 call to subject = new(); , and I wonder if it creates a new instance of Proxy or something else. I did not find anything related to this.

Your help is greatly appreciated.

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

Thanks.

+4
source share
5 answers

This is a typo in the book. There is no current version of C # in which it is valid (it should cause a compiler error "Type expected"). Without context, it is impossible to understand what it should be.

+11
source

AFAIK is wrong and this code does not even compile.

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

+4
source

I was the technical editor of this book; Now I have a copy right in front of me. 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 a mistake; the subject variable has been declared twice. Apparently, I did not catch the error when I looked at the book. (The right thing here is to remove the type from the second "subject" declaration).

However, this is not the error you are reporting.

Are you sure this is not what your copy says? I have a release in December 2007; which edition do you have? Perhaps someone tried to fix this error in a later release and messed it up? I assume that someone tried to fix the error by removing both mentions of type ProtectionProxy from the error string, and not deleting the first.

+4
source

This will be a proxy class. never seen such a syntax before. It’s better not to use things like this to reduce readability.

+2
source

I do not see how this compiles. "new" in this context is a new operator, and it always expects a type name for C # syntax.

+2
source

All Articles