Strings and other base types work a little differently. Certain types that we think would be objects are actually structures, like int's.
So, what is the value of strString in this case? The compiler fakes it by taking care of initializing it for us so that it will return an empty or null string. It is still better to initialize your variable like string strString = string.Empty;
You can do myObject myO;, but it will be completely useless because it is null. So essentially this is the same as a string if you don't initialize it, there's nothing there. when you say strString = "Hi There";, you are essentially doing the same thing
as myO = new myObject;
Some authors don't illustrate always initializing variables, so you can see string myString; in a lot of samples or books, but things are different when doing university tracks since professors will drill into you that this is not the way to create variables,
you always, always initialize right away, either as a value or null. The reason is you never want to leave to chance what the variable will be. It may take less lines of code not to do this, but it's exactly how so many bugs get into code to start with.
So even though you can get away with saying string strString; avoid it at all costs. Set your variables to something so you always know where you start with and don't have to worry if the runtime will handle it differently than you expect at some point in
the future.
Don't forget to mark useful responses as Answer if they helped you towards a solution.
markfitzme
Star
14319 Points
2215 Posts
Re: When do you not have to use keyword new to create object instance
Aug 12, 2012 09:17 PM|LINK
Strings and other base types work a little differently. Certain types that we think would be objects are actually structures, like int's.
So, what is the value of strString in this case? The compiler fakes it by taking care of initializing it for us so that it will return an empty or null string. It is still better to initialize your variable like string strString = string.Empty;
You can do myObject myO;, but it will be completely useless because it is null. So essentially this is the same as a string if you don't initialize it, there's nothing there. when you say strString = "Hi There";, you are essentially doing the same thing as myO = new myObject;
Some authors don't illustrate always initializing variables, so you can see string myString; in a lot of samples or books, but things are different when doing university tracks since professors will drill into you that this is not the way to create variables, you always, always initialize right away, either as a value or null. The reason is you never want to leave to chance what the variable will be. It may take less lines of code not to do this, but it's exactly how so many bugs get into code to start with.
So even though you can get away with saying string strString; avoid it at all costs. Set your variables to something so you always know where you start with and don't have to worry if the runtime will handle it differently than you expect at some point in the future.