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); } }
Author: coder
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; } }
Using Integers
using System; public class UsingIntegers { static void Main(string[] args) { int MyInt = 12345; long MyLong = MyInt; } public long My2ndFunction(long MyLong) { try { long c = checked(MyLong * 500); } catch (OverflowException e) { Console.WriteLine(e); } return 0; } }
Some Operator on int
using System; public class MyMainClass10 { static void Main(string[] args) { int a,b,c,d,e,f; a = 1; b = a + 6; c = b - 3; d = c * 2; e = d / 2; f = e % 2; } }
displays a conversion table of Fahrenheit to Celsius
/*
This program displays a conversion
table of Fahrenheit to Celsius.
Call this program FtoCTable.cs.
*/
using System;
public class FtoCTable {
public static void Main() {
double f, c;
int counter;
counter = 0;
for(f = 0.0; f < 100.0; f++) {
c = 5.0 / 9.0 * (f - 32.0); // convert to Celsius
Console.WriteLine(f + " degrees Fahrenheit is " +
c + " degrees Celsius.");
counter++;
// every 10th line, print a blank line
if(counter == 10) {
Console.WriteLine();
counter = 0; // reset the line counter
}
}
}
}
[/csharp]
Size of int, double, char and bool
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // Sizes.cs -- returns the sixes of C# data types. // Compile with the following command line: // csc /unsafe sizes.cs // namespace nsSizes { using System; struct TestStruct { int x; double y; char ch; } public class Sizes { static public unsafe void Main () { Console.WriteLine ("The size of a bool is " + sizeof (bool)); Console.WriteLine ("The size of a char is " + sizeof (char)); Console.WriteLine ("The size of an int is " + sizeof (int)); Console.WriteLine ("The size of a long is " + sizeof (long)); Console.WriteLine ("The size of an double is " + sizeof (double)); Console.WriteLine ("The size TestStruct is " + sizeof (TestStruct)); } } }
Calculate: int and double
/* 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; } } }