My Profile

Friday, January 28, 2011

Example Program

Example program for printing sum of two numbers


using System;
class VarsDemo
{
       static void Main()
       {
              int x, y, z;
              Console.Write("Enter x Value :");
              x = int.Parse(Console.ReadLine());
              Console.Write("Enter y Value :");
              y = int.Parse(Console.ReadLine());
              z = x + y;
              Console.WriteLine("sum of {0} & {1} is {2}",x,y,z);              
       }
}




Parse is a method under the types, which can convert the string value into an appropriate type on which the method is called.


Examples:



  • int x = "100";                    //invalid
  • int x = int.Parse("100");    //valid
  • bool b = bool.Parse("true");   //valid
  • float f = float.Parse("3.14f");   //valid

using the parse method on an incompatible value to perform the conversion will give an error

Example:

  • int y = int.Parse("I0A");   //Error

----------------------------------------------------------

** If you like this post please like our page in facebook "Dot Net Support

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More