Friday, December 30, 2011

System.Console is a Static Class

Comment: This is a post i am porting from another blog! An old note.

System.Console.WriteLine("Begin Blog");
int port = 12345;
System.Console.WriteLine("Random Port Number:{0}", port);
System.Console.WriteLine("Did you know that TCP/IP implementation deals with " +
"16 bit port addresses? But the int datatype is 32 bit long? \n");
System.Console.WriteLine({0},sizeof(int));
System.Console.WriteLine("Use an Unsigned Short Integer instead..\t");
System.Console.WriteLine("As we know that all port addresses are positive integers and are 16 bits");
System.Console.WriteLine("Press a Key to continue..");
System.Console.Clear();
System.Console.ReadKey();
System.Console.WriteLine("Well, Another interesting thing is..");
System.Console.WriteLine("System.Console is a \"static\" class!!!\n");
System.Console.WriteLine("End Of Blog");


Place your cursor in your code editor window(VS), on the System.Console.WriteLine() method and right-click and select go to definition.

You should see that System.Console class is declared static. All its members are also static.

What this means is we do not create an instance of the System.Console type in our type. We just use it.

Try to create an instance of System.Console.

System.Console console = new System.Console();

You will have a couple of errors.

Error 1 Cannot declare a variable of static type 'System.Console'
Error 2 Cannot create an instance of the static class 'System.Console'


Also note that all the members are also static. A static class can only have
static members.

Try creating a static class with non-static members.

Another example of a static class is System.Environment.


http://msdn.microsoft.com/en-us/library/79b3xss3%28VS.80%29.aspx


http://msdn.microsoft.com/en-us/library/ms229038%28v=VS.80%29.aspx



No comments:

Post a Comment