Skip to Main Content
Pro C# 2010 and the .NET 4 Platform, Fifth Edition
book

Pro C# 2010 and the .NET 4 Platform, Fifth Edition

by Andrew Troelsen
May 2010
Intermediate to advanced content levelIntermediate to advanced
1752 pages
41h 17m
English
Apress
Content preview from Pro C# 2010 and the .NET 4 Platform, Fifth Edition

3.6. Narrowing and Widening Data Type Conversions

Now that you understand how to interact with intrinsic C# data types, let's examine the related topic of data type conversion. Assume you have a new Console Application project named TypeConversions that defines the following class:

class Program
{
  static void Main(string[] args)
  {
    Console.WriteLine("***** Fun with type conversions *****");

    // Add two shorts and print the result.
    short numb1 = 9, numb2 = 10;
    Console.WriteLine("{0} + {1} = {2}",
      numb1, numb2, Add(numb1, numb2));
    Console.ReadLine();
  }

  static int Add(int x, int y)
  { return x + y; }
}

Notice that the Add() method expects to be sent two int parameters. However, the Main() method is, in fact, sending in two short variables. While this ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Professional C# 5.0 and .NET 4.5.1

Professional C# 5.0 and .NET 4.5.1

Christian Nagel, Jay Glynn, Morgan Skinner
Visual C++ 2008: How to Program, Second Edition

Visual C++ 2008: How to Program, Second Edition

P. J. Deitel, H. M. Deitel, D. T. Quirk

Publisher Resources

ISBN: 9781430225492Purchase book