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);
}
}
Software Development
software development notes, main category for Cs, Java, and other programming experiences.
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");
}
}
}
}
Convert Hex char To Int
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Text;
namespace Newtonsoft.Json.Utilities
{
internal class MathUtils
{
public static int HexToInt(char h)
{
if ((h >= '0') && (h <= '9'))
{
return (h - '0');
}
if ((h >= 'a') && (h <= 'f'))
{
return ((h - 'a') + 10);
}
if ((h >= 'A') && (h <= 'F'))
{
return ((h - 'A') + 10);
}
return -1;
}
}
}
[/csharp]