static void Main(string[] args)
{
SystemArrayFunctionality();
}
static void SystemArrayFunctionality()
{
Console.WriteLine("=> Working with System.Array");
//initialize items at startup
string[] gothicBands = { "Tones on Tail", "Bauhaus", "Sisters of Mercy" };
//print out names in declared order
Console.WriteLine("-> Here is the array:");
for (int i = 0; i < gothicBands.Length; i++)
{
//print a name
Console.Write(gothicBands[i] + ", ");
}
Console.WriteLine("\n");
//reverse them
Array.Reverse(gothicBands);
Console.WriteLine("-> The reversed array");
//...and print them
for (int i = 0; i < gothicBands.Length; i++)
{
//print a name
Console.Write(gothicBands[i] + ", ");
}
Console.WriteLine("\n");
//clear out all but the final member
Console.WriteLine("-> Cleared out all but one...");
Array.Clear(gothicBands, 1, 2);
for (int i = 0; i < gothicBands.Length; i++)
{
//print a name
Console.Write(gothicBands[i] + ", ");
}
Console.WriteLine();
}
vampireCoding
Wednesday, February 1, 2012
Friday, January 27, 2012
Friday 1-27-12
class Program
{
static void Main(string[] args)
{
RectMultidimensionalArray();
JaggedMultidimensionalArray();
GenerateArray();
PassAndReceiveArrays();
}
static void RectMultidimensionalArray()
{
Console.WriteLine("=>Rectangular multidimensional array.");
//a rectangular md array
int[,] myMatrix;
myMatrix = new int[6, 6];
//populate (6 *6_ array
for (int i = 0; i < 6; i++)
for (int j = 0; j < 6; j++)
myMatrix[i, j] = i * j;
//print (6*6) array
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
{
Console.Write(myMatrix[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine();
}
static void JaggedMultidimensionalArray()
{
Console.WriteLine("=> Jagged multidimensional array.");
//a jagged MD array (i.e. an array of arrays)
//here we have an array of 5 different arrays
int[][] myJagArray = new int[5][];
//create the jagged array
for (int i = 0; i < 5; i++)
{
myJagArray[i] = new int[i + 7];
}
//print each row (remember, each element is defaulted to zero!)
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < myJagArray[i].Length; j++)
{
Console.Write(myJagArray[i][j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
static void GenerateArray()
{
int[] myArray = new int[5];
myArray[0] = 15;
myArray[1] = 464;
myArray[2] = 978;
myArray[3] = 6;
myArray[4] = 812;
PrintArray(myArray);
Console.WriteLine();
}
//arrays as arguments or return values
static void PrintArray(int[] myInts)
{
for (int i = 0; i < myInts.Length; i++)
Console.WriteLine("Item {0} is {1}", i, myInts[i]);
}
static string[] GetStringArray()
{
string[] theStrings = { "Hello", "from", "GetStringArray" };
return theStrings;
}
static void PassAndReceiveArrays()
{
Console.WriteLine("=> Arrays as params and return values.");
//pass array as parameter
int[] ages = { 20, 22, 23, 0 };
PrintArray(ages);
//get array as return value
string[] strs = GetStringArray();
foreach (string s in strs)
Console.WriteLine(s);
Console.WriteLine();
}
}
{
static void Main(string[] args)
{
RectMultidimensionalArray();
JaggedMultidimensionalArray();
GenerateArray();
PassAndReceiveArrays();
}
static void RectMultidimensionalArray()
{
Console.WriteLine("=>Rectangular multidimensional array.");
//a rectangular md array
int[,] myMatrix;
myMatrix = new int[6, 6];
//populate (6 *6_ array
for (int i = 0; i < 6; i++)
for (int j = 0; j < 6; j++)
myMatrix[i, j] = i * j;
//print (6*6) array
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
{
Console.Write(myMatrix[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine();
}
static void JaggedMultidimensionalArray()
{
Console.WriteLine("=> Jagged multidimensional array.");
//a jagged MD array (i.e. an array of arrays)
//here we have an array of 5 different arrays
int[][] myJagArray = new int[5][];
//create the jagged array
for (int i = 0; i < 5; i++)
{
myJagArray[i] = new int[i + 7];
}
//print each row (remember, each element is defaulted to zero!)
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < myJagArray[i].Length; j++)
{
Console.Write(myJagArray[i][j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
static void GenerateArray()
{
int[] myArray = new int[5];
myArray[0] = 15;
myArray[1] = 464;
myArray[2] = 978;
myArray[3] = 6;
myArray[4] = 812;
PrintArray(myArray);
Console.WriteLine();
}
//arrays as arguments or return values
static void PrintArray(int[] myInts)
{
for (int i = 0; i < myInts.Length; i++)
Console.WriteLine("Item {0} is {1}", i, myInts[i]);
}
static string[] GetStringArray()
{
string[] theStrings = { "Hello", "from", "GetStringArray" };
return theStrings;
}
static void PassAndReceiveArrays()
{
Console.WriteLine("=> Arrays as params and return values.");
//pass array as parameter
int[] ages = { 20, 22, 23, 0 };
PrintArray(ages);
//get array as return value
string[] strs = GetStringArray();
foreach (string s in strs)
Console.WriteLine(s);
Console.WriteLine();
}
}
Wednesday, January 25, 2012
Wednesday 1-25-12
class Program
{
static void Main(string[] args)
{
ShowFolders();
Console.WriteLine();
ShowFolders(@"C:\");
}
static void ShowFolders(string root = @"C:\", bool showFullPath = false)
{
foreach (string folder in Directory.EnumerateDirectories(root))
{
string output = showFullPath ? folder : Path.GetFileName(folder);
Console.WriteLine(output);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace fromTheWeb
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("My machine name: {0}", Environment.MachineName);
Console.WriteLine();
//get drives availabe on local computer and form into a single character expression
string[] drives = Environment.GetLogicalDrives();
string driveNames = string.Empty;
foreach (string drive in drives)
driveNames += drive.Substring(0, 1);
//create regular expression pattern dynamically based on local machine information
string pattern = @"\\\\(?i:" + Environment.MachineName + @")(?:\.\w+)*\\((?i:[" + driveNames + @"]))\$";
string replacement = "$1:";
string[] uncPaths = { @"\\" + Environment.MachineName + @".domain1.mycompany.com\C$\Thingstodo.txt",
@"\\" + Environment.MachineName + @"\c$\ThingsToDo.txt",
@"\\" + Environment.MachineName + @"\d$\documents\mydocument.docx"};
foreach (string uncPath in uncPaths)
{
Console.WriteLine("Input String: " + uncPath);
Console.WriteLine("Returned String: " + Regex.Replace(uncPath, pattern, replacement));
Console.WriteLine();
}
}
}
}
{
static void Main(string[] args)
{
ShowFolders();
Console.WriteLine();
ShowFolders(@"C:\");
}
static void ShowFolders(string root = @"C:\", bool showFullPath = false)
{
foreach (string folder in Directory.EnumerateDirectories(root))
{
string output = showFullPath ? folder : Path.GetFileName(folder);
Console.WriteLine(output);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace fromTheWeb
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("My machine name: {0}", Environment.MachineName);
Console.WriteLine();
//get drives availabe on local computer and form into a single character expression
string[] drives = Environment.GetLogicalDrives();
string driveNames = string.Empty;
foreach (string drive in drives)
driveNames += drive.Substring(0, 1);
//create regular expression pattern dynamically based on local machine information
string pattern = @"\\\\(?i:" + Environment.MachineName + @")(?:\.\w+)*\\((?i:[" + driveNames + @"]))\$";
string replacement = "$1:";
string[] uncPaths = { @"\\" + Environment.MachineName + @".domain1.mycompany.com\C$\Thingstodo.txt",
@"\\" + Environment.MachineName + @"\c$\ThingsToDo.txt",
@"\\" + Environment.MachineName + @"\d$\documents\mydocument.docx"};
foreach (string uncPath in uncPaths)
{
Console.WriteLine("Input String: " + uncPath);
Console.WriteLine("Returned String: " + Regex.Replace(uncPath, pattern, replacement));
Console.WriteLine();
}
}
}
}
Sunday, January 22, 2012
Sunday 1-22-12
static void Main(string[] args)
{
ShowFolders();
}
static void ShowFolders(string root = @"C:\", bool showFullpath = false)
{
foreach (string folder in Directory.EnumerateDirectories(root))
{
string output = showFullpath ? folder : Path.GetFileName(folder);
Console.WriteLine(output);
}
}
{
ShowFolders();
}
static void ShowFolders(string root = @"C:\", bool showFullpath = false)
{
foreach (string folder in Directory.EnumerateDirectories(root))
{
string output = showFullpath ? folder : Path.GetFileName(folder);
Console.WriteLine(output);
}
}
Saturday, January 21, 2012
Saturday 1-21-12
namespace GeneralCoding
{
class Program
{
static void Main(string[] args)
{
DeferType();
ConditionalOperator();
}
class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Address {get; set;}
}
class Company
{
public int Id {get; set;}
public string Name { get; set; }
public bool isBig {get; set;}
}
static void DeferType()
{
//declare three types that have nothing to do with each other
//but all have an Id and name property
Person p = new Person() { Id = 1, Name = "Ben", Address="Redmond, WA"};
Company c = new Company() { Id = 1313, Name="Microsoft", isBig = true};
var v = new {Id = 13, Name="Widget", Silly = true};
PrintInfo(p);
PrintInfo(c);
PrintInfo(v);
}
static void PrintInfo(dynamic data)
{
//will print anythign that has an Id and Name property
Console.WriteLine("ID: {0}, Name: {1}", data.Id, data.Name);
}
//declare an array of objects
static void ArrayDeclaration()
{
//these are all equivalent
int[] array1 = new int[4];
array1[0] = 13; array1[1] = 15; array1[2] = 15; array1[3] = 16;
int[] array2 = new int[4] {13, 14, 15, 16};
int[] array3 = new int[] {13, 14, 15, 16};
int[] array4 = {13, 14, 15, 16};
}
static void ConditionalOperator()
{
bool condition = true;
int x = condition ? 13 : 14;
Console.WriteLine("x is {0}", x);
//you can also embed the condition in other statements
Console.WriteLine("Condition is {0}", condition ? "TRUE" : "FALSE");
}
}
}
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 100; ++i)
{
if (i.IsPrime())
{
Console.WriteLine(i);
}
}
Console.ReadKey();
}
}
static class IntMethods
{
//extension methods must be static
//the this keyword tells C# that this is an extension method
public static bool IsPrime(this int number)
{
//check for evenness
if (number % 2 == 0)
{
if (number == 2)
return true;
return false;
}
//don't need to check past the square
int max = (int)Math.Sqrt(number);
for (int i= 3; i <= max; i += 2)
{
if ((number % i) == 0)
{
return false;
}
}
return true;
}
}
{
class Program
{
static void Main(string[] args)
{
DeferType();
ConditionalOperator();
}
class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Address {get; set;}
}
class Company
{
public int Id {get; set;}
public string Name { get; set; }
public bool isBig {get; set;}
}
static void DeferType()
{
//declare three types that have nothing to do with each other
//but all have an Id and name property
Person p = new Person() { Id = 1, Name = "Ben", Address="Redmond, WA"};
Company c = new Company() { Id = 1313, Name="Microsoft", isBig = true};
var v = new {Id = 13, Name="Widget", Silly = true};
PrintInfo(p);
PrintInfo(c);
PrintInfo(v);
}
static void PrintInfo(dynamic data)
{
//will print anythign that has an Id and Name property
Console.WriteLine("ID: {0}, Name: {1}", data.Id, data.Name);
}
//declare an array of objects
static void ArrayDeclaration()
{
//these are all equivalent
int[] array1 = new int[4];
array1[0] = 13; array1[1] = 15; array1[2] = 15; array1[3] = 16;
int[] array2 = new int[4] {13, 14, 15, 16};
int[] array3 = new int[] {13, 14, 15, 16};
int[] array4 = {13, 14, 15, 16};
}
static void ConditionalOperator()
{
bool condition = true;
int x = condition ? 13 : 14;
Console.WriteLine("x is {0}", x);
//you can also embed the condition in other statements
Console.WriteLine("Condition is {0}", condition ? "TRUE" : "FALSE");
}
}
}
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 100; ++i)
{
if (i.IsPrime())
{
Console.WriteLine(i);
}
}
Console.ReadKey();
}
}
static class IntMethods
{
//extension methods must be static
//the this keyword tells C# that this is an extension method
public static bool IsPrime(this int number)
{
//check for evenness
if (number % 2 == 0)
{
if (number == 2)
return true;
return false;
}
//don't need to check past the square
int max = (int)Math.Sqrt(number);
for (int i= 3; i <= max; i += 2)
{
if ((number % i) == 0)
{
return false;
}
}
return true;
}
}
Thursday, January 19, 2012
Thursday 1-19-12
class Program
{
static void Main(string[] args)
{
DeclareImplicitArrays();
ArrayOfObjects();
}
static void DeclareImplicitArrays()
{
//a is really int[]
var a = new[] { 1, 10, 100, 1000 };
Console.WriteLine("a is a: {0}", a.ToString());
//b is really double[]
var b = new[] { 1, 1.4, 2, 2.5 };
Console.WriteLine("b is a: {0}", b.ToString());
//c is really string[]
var c = new[] { " hello", null, " world" };
Console.WriteLine("c is a: {0}", c.ToString());
Console.WriteLine();
}
static void ArrayOfObjects()
{
Console.WriteLine("=> Array of Objects.");
//An array of objects can be anything at all.
object[] myObjects = new object[4];
myObjects[0] = 10;
myObjects[1] = false;
myObjects[2] = new DateTime(1969, 3, 24);
myObjects[3] = "Form & Void";
foreach (object obj in myObjects)
{
//print the type and value for each item in the array
Console.WriteLine("Type: {0}, Value: {1}", obj.GetType(), obj);
}
Console.WriteLine();
}
}
{
static void Main(string[] args)
{
DeclareImplicitArrays();
ArrayOfObjects();
}
static void DeclareImplicitArrays()
{
//a is really int[]
var a = new[] { 1, 10, 100, 1000 };
Console.WriteLine("a is a: {0}", a.ToString());
//b is really double[]
var b = new[] { 1, 1.4, 2, 2.5 };
Console.WriteLine("b is a: {0}", b.ToString());
//c is really string[]
var c = new[] { " hello", null, " world" };
Console.WriteLine("c is a: {0}", c.ToString());
Console.WriteLine();
}
static void ArrayOfObjects()
{
Console.WriteLine("=> Array of Objects.");
//An array of objects can be anything at all.
object[] myObjects = new object[4];
myObjects[0] = 10;
myObjects[1] = false;
myObjects[2] = new DateTime(1969, 3, 24);
myObjects[3] = "Form & Void";
foreach (object obj in myObjects)
{
//print the type and value for each item in the array
Console.WriteLine("Type: {0}, Value: {1}", obj.GetType(), obj);
}
Console.WriteLine();
}
}
Wednesday, January 18, 2012
Wednesday 1.18.12
class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Arrays *****");
SimpleArrays();
ArrayInitialization();
Console.ReadLine();
}
static void SimpleArrays()
{
Console.WriteLine("=> Simple Array Creation.");
//assign an array of ints containing 3 elements {0, 1, 2}
int[] myInts = new int[3];
myInts[0] = 100;
myInts[1] = 200;
myInts[2] = 300;
//now print each value
foreach (int i in myInts)
Console.WriteLine(i);
Console.WriteLine();
//initialize a 100 item string array, indexed {0 - 99}
string[] booksOnDotNet = new string[100];
Console.WriteLine();
}
static void ArrayInitialization()
{
Console.WriteLine("=> Array Initialization.");
//array initialization syntax using the new keyword
string[] stringArray = new string[] { "one", "two", "three" };
Console.WriteLine("stringArray has {0} elements", stringArray.Length);
//array initialization sytnax without using the new keyword
bool[] boolArray = { false, false, true };
Console.WriteLine("boolArray has {0} elements", boolArray.Length);
//array intialization with new keyword and size
int[] intArray = new int[4] { 20, 22, 23, 0 };
Console.WriteLine("intArry has {0} elements", intArray.Length);
Console.WriteLine();
}
}
{
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Arrays *****");
SimpleArrays();
ArrayInitialization();
Console.ReadLine();
}
static void SimpleArrays()
{
Console.WriteLine("=> Simple Array Creation.");
//assign an array of ints containing 3 elements {0, 1, 2}
int[] myInts = new int[3];
myInts[0] = 100;
myInts[1] = 200;
myInts[2] = 300;
//now print each value
foreach (int i in myInts)
Console.WriteLine(i);
Console.WriteLine();
//initialize a 100 item string array, indexed {0 - 99}
string[] booksOnDotNet = new string[100];
Console.WriteLine();
}
static void ArrayInitialization()
{
Console.WriteLine("=> Array Initialization.");
//array initialization syntax using the new keyword
string[] stringArray = new string[] { "one", "two", "three" };
Console.WriteLine("stringArray has {0} elements", stringArray.Length);
//array initialization sytnax without using the new keyword
bool[] boolArray = { false, false, true };
Console.WriteLine("boolArray has {0} elements", boolArray.Length);
//array intialization with new keyword and size
int[] intArray = new int[4] { 20, 22, 23, 0 };
Console.WriteLine("intArry has {0} elements", intArray.Length);
Console.WriteLine();
}
}
Subscribe to:
Posts (Atom)