Data Types And Variables
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DataTypesAndVariables
{
classProgram
{
staticvoid Main(string[] args)
{
Console.WriteLine("A: Variable Lifetime");
Console.WriteLine("B: Minimum and Maximum Values");
Console.WriteLine("C: Formatting With ToString");
Console.WriteLine("D: Decimal Literals");
Console.WriteLine("E: Decimal Data Type");
Console.WriteLine("F: Char Data Type");
Console.WriteLine("G: String Data Type");
Console.WriteLine("H: Boolean Data Type");
Console.WriteLine("I: Widening Conversions");
Console.WriteLine("");
Console.WriteLine("Enter an option <X to exit>:");
string s = Console.ReadLine();
switch (s)
{
case"A":
ShowTotalAmount();
break;
case"B":
MinMaxValues();
break;
case"C":
FormattingWithToString();
break;
case"D":
DecimalLiterals();
break;
case"E":
DecimalDataType();
break;
case"F":
CharDataType();
break;
case"G":
StringDataType();
break;
case"H":
BooleanDataType();
break;
case"I":
WideningConversions();
break;
case"J":
ImplicitExplicitConversions();
break;
default:
Console.WriteLine("Invalid selection. Please select from one of the above.");
break;
}
Console.ReadLine();
}
staticvoid VariableLifetime()
{
int totalAmount = 100;
totalAmount = totalAmount + 100;
}
staticint totalAmount; //This variable is declared at class level
staticvoid VariableScope()
{
totalAmount = 100;
ShowTotalAmount();
}
staticvoid ShowTotalAmount()
{
Console.WriteLine("Total amount is {0}", totalAmount);
}
staticvoid MinMaxValues()
{
//Integer data types
Console.WriteLine("A byte variable can be between {0} and {1}", byte.MinValue, byte.MaxValue);
Console.WriteLine("A sbyte variable can be between {0} and {1}", sbyte.MinValue, sbyte.MaxValue);
Console.WriteLine("A short variable can be between {0} and {1}", short.MinValue, short.MaxValue);
Console.WriteLine("A ushort variable can be between {0} and {1}", ushort.MinValue, ushort.MaxValue);
Console.WriteLine("A int variable can be between {0} and {1}", int.MinValue, int.MaxValue);
Console.WriteLine("A uint variable can be between {0} and {1}", uint.MinValue, uint.MaxValue);
Console.WriteLine("A long variable can be between {0} and {1}", long.MinValue, long.MaxValue);
Console.WriteLine("A ulong variable can be between {0} and {1}", ulong.MinValue, ulong.MaxValue);
//Floating point data types
Console.WriteLine("A single variable can be between {0} and {1}", Single.MinValue, Single.MaxValue);
Console.WriteLine("A double variable can be between {0} and {1}", Double.MinValue, Double.MaxValue);
Console.WriteLine();
//Decimal point data types
Console.WriteLine("A decimal variable can be between {0} and {1}", Decimal.MinValue, Decimal.MaxValue);
Console.WriteLine();
//DateTime data types
Console.WriteLine("A DateTime variable can be between {0} and {1}", DateTime.MinValue, DateTime.MaxValue);
}
staticvoid FormattingWithToString()
{
int totalAmount = 1000;
Console.WriteLine("The value of intVariable is " + totalAmount.ToString());
Console.WriteLine("intVariable in general format is " + totalAmount.ToString("g"));
Console.WriteLine("intVariable in decimal format is " + totalAmount.ToString("d"));
Console.WriteLine("intVariable in number format is " + totalAmount.ToString("n"));
Console.WriteLine("intVariable in currency format is " + totalAmount.ToString("c"));
Console.WriteLine("intVariable in hexadecimal format is " + totalAmount.ToString("x"));
}
staticvoid DecimalLiterals()
{
decimal totalAmount;
totalAmount = 45.61M; //M implicitly converts this value to a double
}
staticvoid DecimalDataType()
{
decimal totalAmount;
totalAmount = 45.61M;
decimal totalPounds, totalPence;
totalPounds = decimal.Truncate(totalAmount);
totalPence = totalAmount - totalPounds;
Console.WriteLine("The restaurant bill is {0:C}", totalAmount);
Console.WriteLine("You pay the {0:C} and I'll pay the {1:C}", totalPounds, totalPence);
Console.WriteLine();
totalAmount = 45.674586748657M;
Console.WriteLine("The total amount is {0}", totalAmount);
Console.WriteLine("Rounded to 0 decimal places, this value is {0}", Decimal.Round(totalAmount));
Console.WriteLine("Rounded to 4 decimal places, this value is {0}", Decimal.Round(totalAmount, 4));
Console.WriteLine();
totalAmount = -46.51M;
Console.WriteLine("Profit last month was {0}", totalAmount);
decimal totalPounds1;
decimal totalPounds2;
totalPounds1 = Decimal.Floor(totalAmount);
totalPounds2 = Decimal.Ceiling(totalAmount);
Console.WriteLine("Rounding down this number is {0}", totalPounds1);
Console.WriteLine("Rounding up this number is {0}", totalPounds2);
}
staticvoid CharDataType()
{
Console.WriteLine("Code point 82 is {0}", char.ConvertFromUtf32(82));
Console.WriteLine("The code point of R is {0}", char.ConvertToUtf32("R", 0));
Console.WriteLine();
char charVariable1 = 'A';
char charVariable2 = '7';
char charVariable3 = '+';
Console.WriteLine("A is a control character: {0}", char.IsControl(charVariable1));
Console.WriteLine("A is a digit: {0}", char.IsDigit(charVariable1));
Console.WriteLine("A is a letter: {0}", char.IsLetter(charVariable1));
Console.WriteLine("A is lowercase: {0}", char.IsLower(charVariable1));
Console.WriteLine("7 is a number: {0}", char.IsNumber(charVariable2));
Console.WriteLine("7 is punctuation: {0}", char.IsPunctuation(charVariable2));
Console.WriteLine("+ is a separator: {0}", char.IsSeparator(charVariable3));
Console.WriteLine("+ is white space: {0}", char.IsWhiteSpace(charVariable3));
}
staticvoid StringDataType()
{
string greeting1 = "Hello \"Robert\"";
string greeting2 = @"Hello ""Robert""";
Console.WriteLine(greeting1);
Console.WriteLine(greeting2);
Console.WriteLine();
Console.WriteLine("The file is on \\server\allcode."); // the \a is the code for a beep
Console.WriteLine("The file is on \\\\server\\allcode.");
Console.WriteLine(@"The file is on \\server\allcode.");
Console.WriteLine();
}
staticvoid BooleanDataType()
{
int firstVariable = 7;
int secondVariable = 6;
if (firstVariable > secondVariable)
{
Console.WriteLine("{0} is greater than {1}", firstVariable, secondVariable);
}
}
staticvoid ObjectDataType()
{
object anything;
anything = 65.57; //store a double
Console.WriteLine("{0} is {1}", anything, anything.GetType());
anything = "A";
Console.WriteLine("{0} is {1}", anything, anything.GetType());
}
staticvoid ImplicitExplicitConversions()
{
//Implicit conversion of an integer to a decimal
//int unitsOrdered = 100;
//decimal unitPrice = 24.1M;
//decimal totalAmount;
//totalAmount = unitsOrdered * unitPrice;
//Implicit conversion of an integer to a decimal will fail
int unitsOrdered = 100;
decimal unitPrice = 24.1M;
int totalAmount;
//totalAmount = unitsOrdered * unitPrice; //This will fail!
totalAmount = (int)(unitsOrdered * unitPrice);//We need to add int as an implicit cast
}
staticvoid WideningConversions()
{
ConversionOperators();
Console.WriteLine();
Console.WriteLine("Do the same with the Convert class");
Console.ReadKey();
//Use the Convert class
ConvertClass();
Console.WriteLine();
Console.WriteLine("Do the same with the Parse method");
Console.ReadKey();
//Use the Parse method
ParseMethod();
}
staticvoid ConversionOperators()
{
byte byteValue;
short shortValue;
int integerValue;
long longValue;
float floatValue;
double doubleValue;
decimal decimalValue;
char charValue;
string stringValue;
byteValue = 2;
shortValue = 100;
Console.WriteLine("byteValue is {0}, shortValue is {1}", byteValue, shortValue);
shortValue =(short)(shortValue + byteValue); // C# default is integer when adding two values together, so we need to cast as short
Console.WriteLine("Add them and shortValue is {0}", shortValue);
Console.WriteLine();
shortValue = 100;
integerValue = 1000;
Console.WriteLine("shortValue is {0}, integerValue is {1}", shortValue, integerValue);
integerValue = integerValue + (int)shortValue;
Console.WriteLine("Add them and the integer value is {0}", integerValue);
Console.WriteLine();
integerValue = 1000;
longValue = 3000000;
Console.WriteLine("integerValue is {0}, and longValue is {1}", integerValue, longValue);
longValue = longValue + (long)integerValue;
Console.WriteLine("Add them and the longValue is {0}", longValue);
Console.WriteLine();
longValue = 3000000;
floatValue = 2000.12341F;
Console.WriteLine("longValue is {0}, floatValue is {1}", longValue, floatValue);
floatValue = floatValue + (float)longValue;
Console.WriteLine("Add them and floatValue is {0}", floatValue);
Console.WriteLine();
floatValue = 2000.12341F;
doubleValue = 50000.1234;
Console.WriteLine("floatValue is {0}, and doubleValue is {1}", floatValue, doubleValue);
doubleValue = doubleValue + (double)floatValue;
Console.WriteLine("Add them and doubleValue is {0}", doubleValue);
Console.WriteLine();
doubleValue = 50000.1234;
decimalValue = 6000000.1234M;
Console.WriteLine("doubleValue is {0}, decimalValue is {1}", doubleValue, decimalValue);
decimalValue = decimalValue + (decimal)doubleValue;
Console.WriteLine("Add them and decimalValue is {0}", decimalValue);
Console.WriteLine();
decimalValue = 6000000.1234M;
stringValue = "ABC";
Console.WriteLine("decimalValue is {0}, stringValue is {1}", decimalValue, stringValue);
stringValue = stringValue + decimalValue.ToString();
Console.WriteLine("Add them together and stringValue is {0}", stringValue); //This will concatenate the two
Console.WriteLine();
charValue = 'A';
stringValue = "ABC";
Console.WriteLine("charValue is {0}, stringValue is {1}", charValue, stringValue);
stringValue = stringValue + charValue.ToString();
Console.WriteLine("Add them together and stringValue is {0}", stringValue);
Console.WriteLine();
}
staticvoid ConvertClass()
{
byte byteValue;
short shortValue;
int integerValue;
long longValue;
float floatValue;
double doubleValue;
decimal decimalValue;
byteValue = 2;
shortValue = 100;
Console.WriteLine("byteValue is {0}, shortValue is {1}", byteValue, shortValue);
shortValue =Convert.ToInt16(shortValue + byteValue);
Console.WriteLine("Add them and shortValue is {0}", shortValue);
Console.WriteLine();
shortValue = 100;
integerValue = 1000;
Console.WriteLine("shortValue is {0}, integerValue is {1}", shortValue, integerValue);
integerValue = integerValue + Convert.ToInt32(shortValue);
Console.WriteLine("Add them and the integer value is {0}", integerValue);
Console.WriteLine();
integerValue = 1000;
longValue = 3000000;
Console.WriteLine("integerValue is {0}, and longValue is {1}", integerValue, longValue);
longValue = longValue + Convert.ToInt32(integerValue);
Console.WriteLine("Add them and the longValue is {0}", longValue);
Console.WriteLine();
longValue = 3000000;
floatValue = 2000.12341F;
Console.WriteLine("longValue is {0}, floatValue is {1}", longValue, floatValue);
floatValue = floatValue + Convert.ToSingle(longValue);
Console.WriteLine("Add them and floatValue is {0}", floatValue);
Console.WriteLine();
floatValue = 2000.12341F;
doubleValue = 50000.1234;
Console.WriteLine("floatValue is {0}, and doubleValue is {1}", floatValue, doubleValue);
doubleValue = doubleValue + Convert.ToDouble(floatValue);
Console.WriteLine("Add them and doubleValue is {0}", doubleValue);
Console.WriteLine();
doubleValue = 50000.1234;
decimalValue = 6000000.1234M;
Console.WriteLine("doubleValue is {0}, decimalValue is {1}", doubleValue, decimalValue);
decimalValue = decimalValue + Convert.ToDecimal(doubleValue);
Console.WriteLine("Add them and decimalValue is {0}", decimalValue);
Console.WriteLine();
}
staticvoid ParseMethod()
{
byte byteValue;
short shortValue;
int integerValue;
long longValue;
float floatValue;
double doubleValue;
decimal decimalValue;
char charValue;
string stringValue;
byteValue = 2;
shortValue = (short)100; //Explicitly convert this up front. C# needs to understand that this is not an integer
Console.WriteLine("byteValue is {0}, shortValue is {1}", byteValue, shortValue);
shortValue = Int16.Parse((shortValue + byteValue).ToString());
Console.WriteLine("Add them and shortValue is {0}", shortValue);
Console.WriteLine();
shortValue = 100;
integerValue = 1000;
Console.WriteLine("shortValue is {0}, integerValue is {1}", shortValue, integerValue);
integerValue = integerValue + Int32.Parse(shortValue.ToString());
Console.WriteLine("Add them and the integer value is {0}", integerValue);
Console.WriteLine();
integerValue = 1000;
longValue = 3000000;
Console.WriteLine("integerValue is {0}, and longValue is {1}", integerValue, longValue);
longValue = longValue + Int64.Parse(integerValue.ToString());
Console.WriteLine("Add them and the longValue is {0}", longValue);
Console.WriteLine();
longValue = 3000000;
floatValue = 2000.12341F;
Console.WriteLine("longValue is {0}, floatValue is {1}", longValue, floatValue);
floatValue = floatValue + Single.Parse(longValue.ToString());
Console.WriteLine("Add them and floatValue is {0}", floatValue);
Console.WriteLine();
floatValue = 2000.12341F;
doubleValue = 50000.1234;
Console.WriteLine("floatValue is {0}, and doubleValue is {1}", floatValue, doubleValue);
doubleValue = doubleValue + Double.Parse(floatValue.ToString());
Console.WriteLine("Add them and doubleValue is {0}", doubleValue);
Console.WriteLine();
doubleValue = 50000.1234;
decimalValue = 6000000.1234M;
Console.WriteLine("doubleValue is {0}, decimalValue is {1}", doubleValue, decimalValue);
decimalValue = decimalValue + Decimal.Parse(doubleValue.ToString());
Console.WriteLine("Add them and decimalValue is {0}", decimalValue);
Console.WriteLine();
}
staticvoid ValueAndReferenceTypes()
{
}
}
}