/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ namespace nsArea { using System; public class Area { static public void Main () { double Area; int Radius = 42; GetArea (Radius, out Area); Console.WriteLine ("The area of a circle with radius {0} is {1}", Radius, Area); } static void GetArea (int radius, out double area) { const double pi = 3.14159; area = pi * radius * radius; } } }
Assing value to int value
public class Values{ static void Main( ) { int myInt = 7; System.Console.WriteLine("Initialized, myInt: {0}", myInt); myInt = 5; System.Console.WriteLine("After assignment, myInt: {0}",myInt); } }
demonstrates variables
using System; public class Example2DemonstratesVariables { public static void Main() { int x; int y; x = 10; Console.WriteLine("x contains " + x); y = x / 2; Console.Write("y contains x / 2: "); Console.WriteLine(y); } }
Comparing integers using if statements, equality operators, and relational operators.
using System; public class Comparison { public static void Main( string[] args ) { int number1; int number2; Console.Write( "Enter first integer: " ); number1 = Convert.ToInt32( Console.ReadLine() ); Console.Write( "Enter second integer: " ); number2 = Convert.ToInt32( Console.ReadLine() ); if ( number1 == number2 ) Console.WriteLine( "{0} == {1}", number1, number2 ); if ( number1 != number2 ) Console.WriteLine( "{0} != {1}", number1, number2 ); if ( number1 < number2 ) Console.WriteLine( "{0} < {1}", number1, number2 ); if ( number1 > number2 ) Console.WriteLine( "{0} > {1}", number1, number2 ); if ( number1 <= number2 ) Console.WriteLine( "{0} <= {1}", number1, number2 ); if ( number1 >= number2 ) Console.WriteLine( "{0} >= {1}", number1, number2 ); } }
Calculating the product of three integers.
using System; public class Product { public static void Main(string[] args) { int x; int y; int z; int result; Console.Write("Enter first integer: "); x = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter second integer: "); y = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter third integer: "); z = Convert.ToInt32(Console.ReadLine()); result = x * y * z; Console.WriteLine("Product is {0}", result); } }
Call methods from primitive data types
using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { Console.WriteLine(12.GetHashCode()); Console.WriteLine(12.Equals(23)); Console.WriteLine(12.GetType().BaseType); Console.WriteLine(12.ToString()); Console.WriteLine(12); // ToString() called automatically. } }
Assign value to int variable
using System; class UnaryOpsApp { static void Main(string[] args) { int a; a = -42; Console.WriteLine("{0}", a); } }