13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8 <strong>Language</strong> Overview• Two expressions of type object are considered equal if both refer to the same object, or if both arenull.• Two expressions of type string are considered equal if the string instances have identical lengths andidentical characters in each character position, or if both are null.The exampleusing System;class Test{static void Main() {string s = "Test";string t = string.Copy(s);Console.WriteLine(s == t);Console.WriteLine((object)s == (object)t);}}produces the outputTrueFalsebecause the first comparison compares two expressions of type string, and the second comparisoncompares two expressions of type object. (Note that when the Standard Library produces a stringrepresentation of a Boolean value, as is the case with System.WriteLine above, it uses “True” and“False”, while the corresponding <strong>C#</strong> language Boolean literals are spelled true and false.)8.2.2 ConversionsThe predefined types also have predefined conversions. For instance, conversions exist between thepredefined types int and long. <strong>C#</strong> differentiates between two kinds of conversions: implicit conversionsand explicit conversions. Implicit conversions are supplied for conversions that can safely be performedwithout careful scrutiny. For instance, the conversion from int to long is an implicit conversion. Thisconversion always succeeds, and never results in a loss of information. The following exampleusing System;class Test{static void Main() {int intValue = 123;long longValue = intValue;Console.WriteLine("{0}, {1}", intValue, longValue);}}implicitly converts an int to a long.In contrast, explicit conversions are performed with a cast expression. The exampleusing System;class Test{static void Main() {long longValue = Int64.MaxValue;int intValue = (int) longValue;Console.WriteLine("(int) {0} = {1}", longValue, intValue);}}uses an explicit conversion to convert a long to an int. The output is:(int) 9223372036854775807 = -1because an overflow occurs. Cast expressions permit the use of both implicit and explicit conversions.19

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!