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); } }
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
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); } }
Check IndexOutOfRangeException
using System; public class MainEntryPoint { public static void Main() { string userInput; while (true) { try { Console.Write("Input a number between 0 and 5 " + "(or just hit return to exit)> "); userInput = Console.ReadLine(); if (userInput == "") break; int index = Convert.ToInt32(userInput); if (index < 0 || index > 5) throw new IndexOutOfRangeException("You typed in " + userInput); Console.WriteLine("Your number was " + index); } catch (IndexOutOfRangeException e) { Console.WriteLine("Exception: Number should be between 0 and 5. " + e.Message); } catch (Exception e) { Console.WriteLine("An exception was thrown. Message was: " + e.Message); } catch { Console.WriteLine("Some other exception has occurred"); } finally { Console.WriteLine("Thank you"); } } } }