using System; public class TestConsoleApp { public static void Main(string[] args) { Console.WriteLine(123); Console.WriteLine("{0}", 123); Console.WriteLine("{0:D3}", 123); } }
Data Types
Do calculation with int variable
using System; public class MainClass { static void Main(string[] args) { int a; a = -42; Console.WriteLine("{0}", a); int b = 2; int c = 42; a = b * -c; Console.WriteLine("{0}", a); } }
Catch OverflowException Exception
using System; class MainClass { public static void Main() { try { checked { int Integer1; int Integer2; int Sum; Integer1 = 9999999999; Integer2 = 2000000000; Sum = Integer1 + Integer2; } } catch(OverflowException) { Console.WriteLine("A mathematical operation caused an overflow."); } } }
Integer OverFlow
using System; class IntegerOverFlow { static void Main(string[] args) { short s = 32767; s++; Console.WriteLine(s); ushort us = 0; us--; Console.WriteLine(us); } }
Use CultureInfo int ToString method
using System; using System.Globalization; using System.Threading; class Program { static void Main(string[] args) { int val = 1234567890; Console.WriteLine(val.ToString("N")); Console.WriteLine(val.ToString("N",new CultureInfo("fr-FR"))); Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE"); Console.WriteLine(val.ToString("N")); } }
Convert string value to integer by using the int.TryParse
using System; public class MainClass { public static void Main() { int i; bool failure = int.TryParse("qwerty", out i); bool success = int.TryParse("123", out i); } }
Using Variables
using System; public class UsingVariables { static void Main(string[] args) { int MyInt = 12345; int MyInt2 = MyInt + 1; int MyInt3; MyInt3 = My2ndFunction(MyInt2); } static public int My2ndFunction(int myInt2) { myInt2 = myInt2 * 2; return myInt2; } }