Csharp/C Sharp/Data Types/int
Содержание
- 1 Assign value to int variable
- 2 Assing value to int value
- 3 Calculate: int and double
- 4 Calculating the product of three integers.
- 5 Call methods from primitive data types
- 6 Catch OverflowException Exception
- 7 Comparing integers using if statements, equality operators, and relational operators.
- 8 Convert string value to integer by using the int.TryParse
- 9 demonstrates variables
- 10 displays a conversion table of Fahrenheit to Celsius
- 11 Do calculation with int variable
- 12 Format int in Console.WriteLine
- 13 int array property
- 14 int based Fahrenheit and Celsius (Centigrade) Scales
- 15 Integer OverFlow
- 16 Parse int value
- 17 Size of int, double, char and bool
- 18 Some Operator on int
- 19 The precision specifier controls the number of significant digits or zeros to the right of a decimal:
- 20 Use #, % and <zero> in int format
- 21 Use CultureInfo in int.ToString method
- 22 Use CultureInfo int ToString method
- 23 Using Integers
- 24 Using Variables
Assign value to int variable
<source lang="csharp"> using System; class UnaryOpsApp {
static void Main(string[] args) { int a; a = -42; Console.WriteLine("{0}", a); }
}
</source>
Assing value to int value
<source lang="csharp"> 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); } } </source>
Calculate: int and double
<source lang="csharp"> /* 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; } }
}
</source>
Calculating the product of three integers.
<source lang="csharp"> 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); }
}
</source>
Call methods from primitive data types
<source lang="csharp"> 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. }
}
</source>
Catch OverflowException Exception
<source lang="csharp">
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."); } }
}
</source>
Comparing integers using if statements, equality operators, and relational operators.
<source lang="csharp">
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 ); }
}
</source>
Convert string value to integer by using the int.TryParse
<source lang="csharp">
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); }
}
</source>
demonstrates variables
<source lang="csharp"> 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); }
}
</source>
displays a conversion table of Fahrenheit to Celsius
<source lang="csharp"> /*
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 } } }
}
</source>
Do calculation with int variable
<source lang="csharp"> 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); }
}
</source>
Format int in Console.WriteLine
<source lang="csharp"> using System; public class TestConsoleApp {
public static void Main(string[] args) { Console.WriteLine(123); Console.WriteLine("{0}", 123); Console.WriteLine("{0:D3}", 123); }
}
</source>
int array property
<source lang="csharp"> using System; public class SomeClass {
private int[] nums; public SomeClass(int i) { nums = new int[10]; nums[0] = i; } public int[] Nums { get { return nums; } set { nums = value; } }
}
public class MainClass {
public static void Main(string[] args) { SomeClass sc = new SomeClass(42); Console.WriteLine("{0}", sc.Nums[0]); sc.Nums[0] = sc.Nums[0] + 5; Console.WriteLine("{0}", sc.Nums[0]); }
}
</source>
int based Fahrenheit and Celsius (Centigrade) Scales
<source lang="csharp"> using System; public class Class1 {
public static void Main(string[] args) { int nFahr = 100; int nBaseFahr; nBaseFahr = nFahr - 32; int nCelsius; nCelsius = (nBaseFahr * 100) / (212 - 32); Console.WriteLine(nCelsius); }
}
</source>
Integer OverFlow
<source lang="csharp"> using System;
class IntegerOverFlow {
static void Main(string[] args) { short s = 32767; s++; Console.WriteLine(s); ushort us = 0; us--; Console.WriteLine(us); }
}
</source>
Parse int value
<source lang="csharp"> using System; public class Sum {
static void Main(String[] args) { int sum = 0; for (int i=0; i<args.Length; i++) sum += int.Parse(args[i]); Console.WriteLine("The sum is " + sum); }
}
</source>
Size of int, double, char and bool
<source lang="csharp"> /* 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)); } }
}
</source>
Some Operator on int
<source lang="csharp"> 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; }
}
</source>
The precision specifier controls the number of significant digits or zeros to the right of a decimal:
<source lang="csharp"> using System; public class FormatSpecApp {
public static void Main(string[] args) { int i = 123456; Console.WriteLine("{0:C5}", i); Console.WriteLine("{0:D5}", i); Console.WriteLine("{0:E5}", i); Console.WriteLine("{0:F5}", i); Console.WriteLine("{0:G5}", i); Console.WriteLine("{0:N5}", i); Console.WriteLine("{0:P5}", i); Console.WriteLine("{0:X5}", i); }
}
</source>
Use #, % and <zero> in int format
<source lang="csharp"> using System; public class FormatSpecApp {
public static void Main(string[] args) { int i = 123456; Console.WriteLine(); Console.WriteLine("{0:#0}", i); // 123456 Console.WriteLine("{0:#0;(#0)}", i); // 123456 Console.WriteLine("{0:#0;(#0);<zero>}", i); // 123456 Console.WriteLine("{0:#%}", i); // 12345600% i = -123456; Console.WriteLine(); Console.WriteLine("{0:#0}", i); // -123456 Console.WriteLine("{0:#0;(#0)}", i); // (123456) Console.WriteLine("{0:#0;(#0);<zero>}", i); // (123456) Console.WriteLine("{0:#%}", i); // -12345600% i = 0; Console.WriteLine(); Console.WriteLine("{0:#0}", i); // 0 Console.WriteLine("{0:#0;(#0)}", i); // 0 Console.WriteLine("{0:#0;(#0);<zero>}", i); // <zero> Console.WriteLine("{0:#%}", i); // % }
}
</source>
Use CultureInfo in int.ToString method
<source lang="csharp"> using System; using System.Globalization; public class NumParsingApp {
public static void Main(string[] args) { int k = 12345; CultureInfo us = new CultureInfo("en-US"); string v = k.ToString("c", us); Console.WriteLine(v); }
}
</source>
Use CultureInfo int ToString method
<source lang="csharp">
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")); }
}
</source>
Using Integers
<source lang="csharp"> 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; }
}
</source>
Using Variables
<source lang="csharp"> 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; }
}
</source>