Saturday, January 14, 2012

Saturday 1/14/2012

namespace MoreCSharpCoreConst
{
class Program
{
static void Main(string[] args)
{
DeclareExplicitVars();
DeclareImplicitVars();
ForLoop();
ForAndForEachLoop();
ExecuteWhileLoop();
ExecuteDoWhileLoop();
ExecuteSwitch();
ExecuteSwitchOnString();
}

//explicit variables
static void DeclareExplicitVars()
{
//explicitly typed local variables
//are declared as follows:
//datatype variableName = initialValue;
int myInt = 0;
bool myBool = true;
string myString = "Time, marches on...";
}

//implicitly typed variables
static void DeclareImplicitVars()
{
//implicitly typed local variabels are declared as follows:
//var variableName = initialValue;
var myInt = 0;
var myBool = true;
var myString = "time, marches on...";

//print out the underlying type
Console.WriteLine("myInt is a: {0}", myInt.GetType().Name);
Console.WriteLine("myBool is a: {0}", myBool.GetType().Name);
Console.WriteLine("myString is a: {0}", myString.GetType().Name);
}

//-------------Iteration Constructs---------------------------------

//for loop
static void ForLoop()
{
//note 'i' is only visible within the scope of the for loop
for (int i = 0; i < 4; i++)
{
Console.WriteLine("Number is {0} ", i);
}
}

//iterate array items using foreach
static void ForAndForEachLoop()
{
string[] carTypes = { "Ford", "BMW", "Yugo", "Honda" };
foreach (string c in carTypes)
Console.WriteLine(c);

int[] myInts = { 10, 20, 30, 40 };
foreach (int i in myInts)
Console.WriteLine(i);
}

static void ExecuteWhileLoop()
{
string userIsDone = "";
//test on a lower-class copy of the string
while (userIsDone.ToLower() != "yes")
{
Console.WriteLine("Area you done? [yes] [no]: ");
userIsDone = Console.ReadLine();
Console.WriteLine("in while loop");
}
}

static void ExecuteDoWhileLoop()
{
string userIsDone = "";

do
{
Console.WriteLine("In do/while loop");
Console.Write("Are you done? [yes] [no]: ");
userIsDone = Console.ReadLine();
} while (userIsDone.ToLower() != "yes"); //note the semicolon!
}

//switch on a numerical value
static void ExecuteSwitch()
{
Console.WriteLine("1 [C#], 2 [VB]");
Console.WriteLine("Please pick your language preference: ");
string langChoice = Console.ReadLine();
int n = int.Parse(langChoice);

switch (n)
{
case 1:
Console.WriteLine("Good choice, C# is a fine language.");
break;
case 2:
Console.WriteLine("VB: OOP, multithreading, and more!");
break;
default:
Console.WriteLine("well...good luck with that!");
break;
}
}

static void ExecuteSwitchOnString()
{
Console.WriteLine("C# or VB");
Console.Write("Please pick your language preference: ");
string langChoice = Console.ReadLine();
switch (langChoice)
{
case "C#":
Console.WriteLine("Good choice, C# is a fine language.");
break;
case "VB":
Console.WriteLine("VB: OOP, multithreading and more!");
break;
default:
Console.WriteLine("Well... good luck with that!");
break;

}
}

}
}


namespace CoreConstructsPt2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Fun with Methods");
string s1 = "Flip";
string s2 = "Flop";
Console.WriteLine("Before: {0}, {1} ", s1, s2);
SwapStrings(ref s1, ref s2);
Console.WriteLine("After: {0}, {1} ", s1, s2);
Console.WriteLine();
//pass in a comma-delimited list of doubles
double average;
average = CalcAverage(4.0, 3.2, 5.8, 64.22, 87.2);
Console.WriteLine("Average of data is: {0}", average);

//or pass an array of doubles
double[] data = { 4.0, 3.2, 5.7 };
average = CalcAverage(data);
Console.WriteLine("Average of data is: {0}", average);
Console.WriteLine();
DisplayFancyMessage(Message: "Wow! Very fancy indeed!", textColor: ConsoleColor.DarkRed, backgroundColor: ConsoleColor.White);

DisplayFancyMessage(backgroundColor: ConsoleColor.Green, Message: "Testing...", textColor: ConsoleColor.DarkBlue);

//calls int version of add()
Console.WriteLine(add(10, 10));

//calls long version of Add()
Console.WriteLine(add(9000000, 120000000000000000));

//calls double version of Add()
Console.WriteLine(add(4.3, 4.4));

Console.ReadLine();
}


public static void SwapStrings(ref string s1, ref string s2)
{
string tempStr = s1;
s1 = s2;
s2 = tempStr;
}

static double CalcAverage(params double[] values)
{
Console.WriteLine("You sent me {0} doubles.", values.Length);
double sum = 0;
if (values.Length == 0)
return sum;

for (int i = 0; i < values.Length; i++)
sum += values[i];
return (sum / values.Length);
}


//invoking methods using named parameters
static void DisplayFancyMessage(ConsoleColor textColor, ConsoleColor backgroundColor, string Message)
{
//store old colors to restore once message is printed
ConsoleColor oldTextColor = Console.ForegroundColor;
ConsoleColor oldbackgroundColor = Console.BackgroundColor;

//set new colors and print message
Console.ForegroundColor = textColor;
Console.BackgroundColor = backgroundColor;

Console.WriteLine(Message);

//resore previous colors
Console.ForegroundColor = oldTextColor;
Console.BackgroundColor = oldbackgroundColor;
}

static int add(int x, int y)
{ return x + y; }

static double add(double x, double y)
{ return x + y; }

static double add(long x, long y)
{ return x + y; }
}
}


@autoreleasepool {
//create three NSDate objects
NSDate *now = [NSDate date];
NSDate *tomorrow = [now dateByAddingTimeInterval:24.0 * 60.0 * 60.0];
NSDate *yesterday = [now dateByAddingTimeInterval:-24.0 * 60.0 * 60.0];
//create an array containing all three (nil terminates the list);
NSArray *dateList = [NSArray arrayWithObjects:now, tomorrow, yesterday, nil];

//how many dates are there?
NSLog(@"There are %lu dates", [dateList count]);

//print a couple
NSLog(@"The first date is %@", [dateList objectAtIndex:0]);
NSLog(@"The third date is %@", [dateList objectAtIndex:2]);
}



@autoreleasepool {
//create three NSDate objects
NSDate *now = [NSDate date];
NSDate *tomorrow = [now dateByAddingTimeInterval:24.0 * 60.0 * 60.0];
NSDate *yesterday = [now dateByAddingTimeInterval:-24.0 * 60.0 * 60.0];

//create an array containg all three (nil terminates the list)
NSArray *dateList = [NSArray arrayWithObjects:now, tomorrow, yesterday, nil];

NSUInteger dateCount = [dateList count];
for (int i = 0; i < dateCount; i++)
{
NSDate *d = [dateList objectAtIndex:i];
NSLog(@"Here is a date: %@", d);
}

for (NSDate *d in dateList){
NSLog(@"Here is a date: %@", d);
}
}

No comments:

Post a Comment