What does a String Property GET return?

In C#

Assume I have the following:

public String whatHappens{ get; set; }

Assume the property has been initialized and set.

Then down here I call

Console.WriteLine(whatHappens);

Does the GET call return a string object and the Console.WriteLine call ToString() OR Does the GET call in turn call the ToString() method within the string class and return a string.

This is not a school question it's an argument my co-worker and I are having.

Answers


In your example you use an automatic property. An automatic property has an extra hidden field, the backing field. This field is of the same type as the property, in your case a string.

Inside this property the code will not call ToString() since the backing field is already of type string.

Console.WriteLine has many overloads. One of these overloads accepts a parameter of type string. So inside Console.WriteLine there is no need to call ToString(), because the parameter is already of that type.

In short: No ToString() will be called anywhere.

If, on the other hand, the property was not an automatic property and the field (or fields) behind it are of another type, then they would need to be converted to a string, using ToString().

If, on the other hand, another overload of Console.WriteLine was called, where the parameter was of type object, than WriteLine has to call ToString() at some point.


Need Your Help

Windows Identity Foundation (WIF) - ClaimsAuthenticationManager

wif

I have code similar to the snippet below (taken from http://leastprivilege.com/2009/05/24/use-geneva-session-management-for-your-own-needs/)

MySQL Left Join Count Not working

mysql select count left-join

I have 2 tables (members, duty_sched)

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.