// Basic
static void Main(string[] args)
{
string userValue = Console.ReadLine();
string msg = (userValue == "1") ? "new car" : "new job";
Console.WriteLine("You won a {0}", msg);
string zig = "You can get what you want out of life " +
"if you help enough other people get what they want.";
char[] charArray = zig.ToCharArray();
Array.Reverse(charArray);
foreach (char z in charArray)
Console.Write(z + " ");
Console.ReadLine();
}
// Read File
static void Main(string[] args)
{
StreamReader myReader = new StreamReader("Values.txt");
string line = "";
while (line != null)
{
line = myReader.ReadLine();
if (line != null)
Console.WriteLine(line);
}
myReader.Close();
Console.ReadLine();
}
// Working with String
static void Main(string[] args)
{
// string myString = "Go to your c:\\drive";
// string myString1 = "My \"so called\" life";
// string myString = string.Format("{0}!", "Bonzai");
// $ string myString = string.Format("{0:C}", 123.45);
// phone number string myString = string.Format("Phone number: {0:(###) ###-###}", 1234567890);
/* string myString = "";
for (int i = 0; i < 100; i++)
{
myString = myString + "--" + i.ToString();
} */
/* StringBuilder myString = new StringBuilder();
for (int i = 0; i < 100; i++)
{
myString.Append("--");
myString.Append(i);
} */
string myString = "That summer we took threes across the board";
/*
myString = myString.Substring(5, 14);
myString = myString.ToUpper();
myString = myString.Replace(" ", "--");
*/
myString = String.Format("Length before: {0} -- After: {1}",
myString.Length, myString.Trim().Length);
Console.WriteLine(myString);
Console.ReadLine();
}
// Working with DateTime
static void Main(string[] args)
{
DateTime myValue = DateTime.Now;
Console.WriteLine(myValue.ToString());
Console.WriteLine(myValue.ToShortDateString());
Console.WriteLine(myValue.ToShortTimeString());
Console.WriteLine(myValue.ToLongDateString());
Console.WriteLine(myValue.ToLongTimeString());
Console.WriteLine(myValue.AddDays(3).ToLongDateString());
Console.WriteLine(myValue.AddHours(3).ToShortTimeString());
Console.WriteLine(myValue.Month.ToString());
DateTime mybirthday = new DateTime(1991, 12, 05);
Console.WriteLine(mybirthday.ToShortDateString());
DateTime myB = DateTime.Parse("05/12/1991");
TimeSpan myA = DateTime.Now.Subtract(myB);
Console.WriteLine(myA.TotalHours);
Console.ReadLine();
}
// Understanding Object
class Program
{
static void Main(string[] args)
{
Car mycar1 = new Car();
mycar1.Make = "Ford";
mycar1.Model = "Raptor";
mycar1.Color = "Red";
mycar1.Year = 2008;
Truck myTruck = new Truck();
myTruck.Make = "Ford";
myTruck.Model = "F969";
myTruck.Year = 2006;
myTruck.Color = "Black";
myTruck.To = 1200;
Console.WriteLine("Here are the Car's details: {0}", mycar1.FormatMe());
Console.WriteLine("Here are the Truck's details: {0}", myTruck.FormatMe());
Console.ReadLine();
}
}
abstract class Vehicle
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public string Color { get; set; }
public abstract string FormatMe();
}
sealed class Truck : Vehicle
{
public int To { get; set; }
public override string FormatMe()
{
return String.Format("{0}-{1}-{2}",
this.Make,
this.Model,
this.To);
}
}
class Car : Vehicle
{
public override string FormatMe()
{
return String.Format("{0}-{1}-{2}-{3}",
this.Make,
this.Model,
this.Color,
this.Year);
}
}
// Enum Type
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("Type in a superhero's name to see his nickname:");
string userValue = Console.ReadLine();
SuperHero myValue;
if (Enum.TryParse<SuperHero>(userValue, true, out myValue))
{
switch (myValue)
{
case SuperHero.Batman:
Console.WriteLine("Batmaan!");
break;
case SuperHero.Superman:
Console.WriteLine("Man of Steel");
break;
case SuperHero.Green:
Console.WriteLine("Emerald Knight");
break;
default: break;
}
}
Console.ReadLine();
}
}
enum SuperHero
{
Batman,
Superman,
Green
}
}
No comments:
Post a Comment