Вы находитесь на странице: 1из 26

1.

Hello World

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadLine();
}
}
}
2.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LimaVariable
{
class Program
{
static void Main(string[] args)
{
/*
* Pertama
//Deklarasi Variabel
int x;
int y;
//Assign Variabel
x = 7;
y = x + 3;

Lima Variable

Console.WriteLine(y);*/
/*
* Kedua
string myFirstName;
myFirstName = "Aldi";
*/
//string myFirstName = "Aldi";
//var myFirstName = "Aldi";
//Console.WriteLine(myFirstName);
int x = 7;
string y = "Bob";
string y1 = "5";
string myFirstTry = x.ToString() + y1;
int mySecondTry = x + int.Parse(y1);
Console.WriteLine(myFirstTry);
Console.WriteLine(mySecondTry);
Console.ReadLine();
}
}
}
3.

Enam Decison

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _6Decision
{
class Program
{
static void Main(string[] args)
{
/* Pertama
Console.WriteLine("Please type Something and press Enter");
string userValue;
userValue = Console.ReadLine();

Console.WriteLine("You typed : " + userValue);


Console.ReadLine();
*/
Console.WriteLine("Would You prefer what is the behind door number 1, 2 or
3?");
string userValue = Console.ReadLine();
/*Kedua
if (userValue == "1")
{
Console.WriteLine("You Won a new Car!");
Console.ReadLine();
}
else if (userValue == "2")
{
Console.WriteLine("You Won a new Boat!");
Console.ReadLine();
}
else if (userValue == "3")
{
Console.WriteLine("You Won a new Yacht!");
Console.ReadLine();
}
else
{
Console.WriteLine("Sorry, we didnt understand. You Lose!");
Console.ReadLine();
}*/
/*Ketiga
if (userValue == "1")
message ="You Won a new Car!";
else if (userValue == "2")
message ="You Won a new Boat!";
else if (userValue == "3")
message ="You Won a new Yacht!";
else
message ="Sorry, we didnt understand. You Lose!";
*/
string message = (userValue == "1") ? "Boat" : "Strand of lint";
Console.WriteLine("You Won a {0}! And you type {1}", message, userValue);
Console.ReadLine();
}

}
}
4.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TujuhOperator
{
class Program
{
static void Main(string[] args)
{
int x = 0;
int y = 0;
int a = 0;
int b = 0;
//Assignment Operator
x = 3;
//Addition Operator
x = 3 + 4;
//Subtraction operator
x = 4 - 3;
//Multiplication Operator
x = 10 * 3;
//Division Operator
x = 10 / 5;
//Equality Operator
if (x == y)
{
}
//Greater Than Operator
if (x > y)
{

Tujuh Operator

}
//less than Operator
if (y < x)
{
}
//Greater Than or equal to Operator
if (x >= y)
{
}
//less Than or equal to Operator
if (x <= y)
{
}
//Conditiona and Operator
if ((x < y ) && (y > x))
{
}
//Conditional or operator
if ((x < y) || (y > x))
{
}
//Conditional
string message = (x == 1) ? "Car" : "Boat";
//Member Accesc and method invocation
Console.WriteLine("HI!");
Console.ReadLine();
}
}
}
5.

Delapan iterasi

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DelapanIteration
{
class Program
{
static void Main(string[] args)
{
for (int i = 10; i < 0; i--)
{
Console.WriteLine(i.ToString());
}
Console.ReadLine();
}
}
}
6.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SembilanArray
{
class Program
{
static void Main(string[] args)
{
/* Pertama
int[] number = new int[5];
number[0] = 4;
number[1] = 5;
number[2] = 6;
number[3] = 7;
number[4] = 8;

Sembilan Array

for (int i = 0; i < number.Length; i++)


{
Console.WriteLine(number[i].ToString());
} */
//Kedua
int[] number = { 4, 5, 6, 7, 8 };
for (int i = 0; i < number.Length; i++)
{
Console.WriteLine(number[i].ToString());
}
//Ketiga
string[] names = { "Muhammad", "Aldi", "Perdana", "Putra" };
foreach (string name in names)
{
Console.WriteLine(name);
}
//Keempat
string nama = "Muhammad Aldi Perdana Putra";
char[] charArray = nama.ToCharArray();
Array.Reverse(charArray);
foreach (char myChar in charArray)
{
Console.Write(myChar);
}
//Console.WriteLine(number[1].ToString());
Console.ReadLine();
}
}
}
7.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

Sepuluh method

namespace SepuluhMethod
{
class Program
{
static void Main(string[] args)
{
//string myValue = superSecretFormula();
string myValue = superSecretFormula("Bob");
Console.WriteLine(myValue);
myValue = superSecretFormula("Aldi");
Console.WriteLine(myValue);
Console.WriteLine(superSecretFormula());
Console.ReadLine();
}
//Pertama
private static string superSecretFormula()
{
return "Hello World";
}
//Kedua
private static string superSecretFormula(string name)
{
return String.Format("Hello World, {0}",name);
}
}
}
8.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SebelasWhileIteration
{
class Program
{
static void Main(string[] args)

Sebelas while iteration

{
StreamReader myReader = new StreamReader("Sebelas.txt");
string line = "";
while (line != null)
{
line = myReader.ReadLine();
if (line != null)
{
Console.WriteLine(line);
}
}
myReader.Close();
Console.ReadLine();
}
}
}
9.

Duabelas String

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace duabelasiString
{
class Program
{
static void Main(string[] args)
{
//Pertama
//string myString = "Go to your C:\\ drive";
//Kedua
//string myString = "My \" so Called\" Life";
//Ketiga
//string myString = "What if I need \n a new line";
//Keempat

//string myString = string.Format("{0}! {1}", "Hello", "Aldi");


//Kelima
//string myString = string.Format("Make : {0} (Model : {1})", "BMW","745li");
//Keenam
//string myString = string.Format("{0:C}", 123.45);
//Ketujuh
//string myString = string.Format("{0:N}", 123456789);
//Kedelapan
//string myString = string.Format("{0:P}",.123);
//kesembilan
//string myString = string.Format("Phone Number : {0:(###) #######}",1234567890);
//Kesepuluh
//string myString = "";
//kesebelas
/*
StringBuilder myString = new StringBuilder();
for (int i = 0; i < 100; i++)
{
//myString = myString + "--" + i.ToString();
//myString += "--" + i.ToString();
myString.Append("--");
myString.Append(i);
}*/
//keduabelas
string myString = "It's better to be lucky than good. ";
//myString = myString.Substring(5, 12);
//ketigabelas
//myString = myString.ToUpper();
//keempatbelas
myString = myString.Replace(" ", "--");

//kelimabelas
myString = String.Format("Length before: {0} -- after: {1}", myString.Length,
myString.Trim().Length);
Console.Write(myString);
Console.ReadLine();
}
}
}
10. Tigabelas date time
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace tigabelasDatetime
{
class Program
{
static void Main(string[] args)
{
//Pertama
DateTime myValue = DateTime.Now;
//Console.WriteLine(myValue.ToString());
//kedua
//Console.WriteLine(myValue.ToShortDateString());
//ketiga
//Console.WriteLine(myValue.ToShortTimeString());
//keempat
//Console.WriteLine(myValue.AddDays(3).ToShortDateString());
//kelima
//Console.WriteLine(myValue.AddHours(3).ToShortTimeString());
//keenam
//Console.WriteLine(myValue.Month.ToString());
DateTime myBirthday = new DateTime(1994, 07, 06);
TimeSpan myAge = DateTime.Now.Subtract(myBirthday);
Console.WriteLine(myAge.TotalDays.ToString());
Console.ReadLine();

}
}
}
11. Empatbelasclass
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace empatbelasClass
{
class Program
{
static void Main(string[] args)
{
Car myNewCar = new Car();
myNewCar.Make = "Oldsmobile";
myNewCar.Model = "Cutlas Supreme";
myNewCar.Year = 1986;
myNewCar.Color = "Silver";
//Console.WriteLine("{0} - {1} - {2}", myNewCar.Make, myNewCar.Model,
myNewCar.Color);
// determineMarketValue(myNewCar);
myNewCar.DetermineMarketValue();
string myValue = myNewCar.DetermineMarketValue();
Console.WriteLine(myValue);
Console.ReadLine();
}
/*
private static double determineMarketValue( Car car)
{
return 100.0;
}*/
}
class Car

{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public string Color { get; set; }
public string DetermineMarketValue()
{
this.Color = "White";
return this.Color;
}
}
}
12. Limabelas more class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace limabelasMoreClass
{
class Program
{
static void Main(string[] args)
{
Car myNewCar = new Car();
myNewCar.Make = "Toyota";
myNewCar.Model = "4Runner";
myNewCar.Year = 2006;
myNewCar.OriginalPrice = 35000;
myNewCar.Color = "White";
/*
Car myOtherCar = myNewCar;
Console.WriteLine(myOtherCar.Make);*/
//myOtherCar = null;

//myNewCar = null;
//Console.WriteLine("{0} {1} {2} {3}", myNewCar.Make, myNewCar.Model,
myNewCar.Year.ToString(), myNewCar.Color);
Console.WriteLine("Before : " + myNewCar.Make);
doByValue(myNewCar);
Console.WriteLine("After by Value : "+myNewCar.Make);
doByReference( ref myNewCar);
Console.WriteLine("After by Reference : " + myNewCar.Make);
Console.ReadLine();
}
private static double determineMarketValue(Car car)
{
double carValue = (((double)DateTime.Now.Year - (double)car.Year) / 10) *
car.OriginalPrice;
return carValue;
}
private static void doByValue( Car car)
{
car = new Car();
car.Make = "BMW";
}
private static void doByReference( ref Car car)
{
car = new Car();
car.Make = "BMW";
}
}
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public int OriginalPrice { get; set; }
public string Color { get; set; }
}
}
13. Limabelas ++

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace limabelas__
{
class Program
{
static void Main(string[] args)
{
int i = 0;
Console.WriteLine("Before: {0}", i);
doByValue(i);
Console.WriteLine("After By Value: {0}", i);
doByReference(ref i);
Console.WriteLine("After By Reference: {0}", i);
Console.ReadLine();
}
private static void doByValue(int i)
{
i++;
}
private static void doByReference( ref int i)
{
i++;
}
}
}
14. Enambelas class dan pewarisan
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace enambelasClassandInheritance
{
class Program
{

static void Main(string[] args)


{
Car myCar = new Car();
myCar.Make = "BMW";
myCar.Model = "745Li";
myCar.Color = "Black";
myCar.Year = 2005;
Truck myTruck = new Truck();
myTruck.Make = "Ford";
myTruck.Model = "f950";
myTruck.Year = 2006;
myTruck.Color = "Black";
myTruck.TowingCapacity = 1200;
/* Pertama
someMethod(myCar);
someMethod(myTruck);*/
myCar.PrintMe();
myTruck.PrintMe();
Console.ReadLine();
}
private static void someMethod( Car car)
{
Console.WriteLine("From someMethod: {0}", car.Make);
}
}
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 void PrintMe();
}
class Car: Vehicle
{
/*public virtual void PrintMe()
{

Console.WriteLine("{0} - {1}", this.Make, this.Model);


}*/
public override void PrintMe()
{
Console.WriteLine("{0} - {1}", this.Make, this.Model);
}
}
sealed class Truck : Vehicle
{
public int TowingCapacity { get; set; }
public override void PrintMe()
{
Console.WriteLine("{0} - {1}", this.Make, this.TowingCapacity.ToString());
}
}
}
15. Tujuhbelas namespace
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace tujuhbelasNamespace
{
class Program
{
static void Main(string[] args)
{
//System.IO.StreamReader myStreamReader = new System.IO.StreamReader();
//StreamReader myStreamReader = new StreamReeader();
StreamReader myStreamReader = new StreamReader();
}
}
}
16. Delapanbelas scoop
using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace delapanBelasScoop
{
class Program
{
// private static string k = "";
static void Main(string[] args)
{
/*
string j = "";
for (int i = 0; i < 10; i++)
{
j = i.ToString();
k = i.ToString();
Console.WriteLine(i);
if (i == 9)
{
string l = i.ToString();
}
//Console.WriteLine(l);
}
Console.WriteLine("Outside the For Iterattion : " + j);
//Console.WriteLine("K : " + k);
helperMethod();*/
Car car = new Car();
car.DoSomething();
Console.ReadLine();
}
/*
static void helperMethod()
{
Console.WriteLine("K from Helper Method : "+k);
}*/
}
class Car
{
public void DoSomething()
{
Console.WriteLine(helperMethod());
}

private string helperMethod()


{
return "Hello World!";
}
}
}
17. Sembilanbelas switch
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace sembilanbelasSwitch
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Type in a super hero's nam to see his nickname");
string userValue = Console.ReadLine();
switch (userValue)
{
case "Batman":
Console.WriteLine("Caped Crusader");
break;
case "Superman":
Console.WriteLine("Man of Steel");
break;
case "Green Latern":
Console.WriteLine("Emerald Knight");
break;
default:
Console.WriteLine("Does Not Compute");
break;
}
Console.ReadLine();
}
}
}
18. Sembilan belas enum

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace sembilanbelasEnum
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Yellow;
//Superhero myValue = Superhero.Batman;
//Console.WriteLine("Hello World!");
Console.WriteLine("Type in a super hero'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("Caped Crusader");
break;
case Superhero.Superman:
Console.WriteLine("Man Of Steel");
break;
case Superhero.GreenLantern:
Console.WriteLine("Emerald Knight");
break;
default:
Console.WriteLine("Does Not COmpute");
break;
}
}
else
{
Console.WriteLine("Soes Not COmpute");
}
Console.Read();

}
}
enum Superhero
{
Batman,
Superman,
GreenLantern
}
}
19. Duapuluh handling
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SebelasWhileIteration
{
class Program
{
static void Main(string[] args)
{
try
{
StreamReader myReader = new StreamReader("Sebelas11.txt");
string line = "";
while (line != null)
{
line = myReader.ReadLine();
if (line != null)
{
Console.WriteLine(line);
}
}
myReader.Close();
Console.ReadLine();
}
catch (FileNotFoundException e)
{

Console.WriteLine("Couldn't find the file. Are you sure you're looking for the
right file");
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("Couldn't finde the file. Are you sure that directory exist?");
}
catch (Exception e)
{
//Console.WriteLine("We Experienced a Problem. Sorry!");
Console.WriteLine("We Experienced a problem : {0}", e.Message);
}
finally
{
//PErform any cleanup to roll back the data or close connection to file,
database, etc.
}
Console.ReadLine();
}
}
}
20. Duapuluhsatu collection
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace duapuluhsatuCollection
{
class Program
{
static void Main(string[] args)
{
/*
Car myNewCar1 = new Car();
myNewCar1.Make = "OldsMobile";
myNewCar1.Model = "Cutlas Supreme";
Car myNewCar2 = new Car();
myNewCar2.Make = "Geo";

myNewCar2.Model = "Prism";
Book myNewBook1 = new Book();
myNewBook1.Author = "M. Aldi P. Putra";
myNewBook1.Title = "Dia Ubah Dunia";
myNewBook1.ISBN = "7-777-77777-7";*/
/*
//ArrayList are dynamically sized, and support other cool features like sorting,
removing item, etc.
ArrayList myArray = new ArrayList();
myArray.Add(myNewCar1);
myArray.Add(myNewCar2);
myArray.Add(myNewBook1);
//However, everyhting is stored as an object . . . which introduce potential
problems
foreach (object o in myArray)
{
Console.WriteLine(((Car)o).Make);
}*/
/*
ListDictionary myDictionary = new ListDictionary();
myDictionary.Add(myNewCar1.Make, myNewCar1);
myDictionary.Add(myNewCar2.Make, myNewCar2);
myDictionary.Add(myNewBook1.Author, myNewBook1);
//Easy to Acces an element using its key
Console.WriteLine(((Car)myDictionary["Geo"]).Model);
//But Since its not strongly typed, we can easily break it
//By adding a different type to the dictionary
Console.WriteLine(((Car)myDictionary["M. Aldi P. Putra"]).Model);*/
/*
List<Car> myList = new List<Car>();
myList.Add(myNewCar1);
myList.Add(myNewCar2);
foreach (Car car in myList)
{
Console.WriteLine(car.Model);
}*/

/*
Dictionary<string, Car> myDictionary = new Dictionary<string,Car>();
myDictionary.Add(myNewCar1.Make, myNewCar1);
myDictionary.Add(myNewCar2.Make, myNewCar2);
Console.WriteLine(myDictionary["Geo"].Model);*/
//string [] names = {"Muhammad", "Aldi","Perdana","Putra"};
//Object Initializers
/*
Car car1 = new Car() { Make = "Oldsmobile", Model = "Cutlas Supreme" };
Car car2 = new Car() { Make = "Geo", Model = "Prism" };
Car car3 = new Car() { Make = "Nissan", Model = "Altima" };*/
//Collection Initializers
List<Car> myList = new List<Car>(){
new Car {Make = "Oldsmobile", Model ="Cutlas Supreme"},
new Car {Make = "Geo", Model ="Prism"},
new Car {Make = "Nissan", Model ="Altima"},
};
Console.ReadLine();
}
}
class Car
{
public string Make { get; set; }
public string Model { get; set; }
}
class Book
{
public string Title { get; set; }
public string Author { get; set; }
public string ISBN { get; set; }
}
}
21. Duapuluhdua linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading.Tasks;
namespace duapuluhduaLinq
{
class Program
{
static void Main(string[] args)
{
List<Car> myCars = new List<Car>{
new Car(){Make = "BMW", Model="550i", Color = CarColor.Blue,
StickerPrice = 55000, Year = 2009},
new Car(){Make = "Toyota", Model="4Runner", Color = CarColor.White,
StickerPrice = 35000, Year = 2010},
new Car(){Make = "BMW", Model="745Li", Color = CarColor.Black,
StickerPrice = 75000, Year = 2008},
new Car(){Make = "Ford", Model="Escape", Color = CarColor.White,
StickerPrice = 28000, Year = 2008},
new Car(){Make = "BMW", Model="550i", Color = CarColor.Black,
StickerPrice = 57000, Year = 2010},
};
//We Add Code here
//Pertama
var bmws = from car in myCars
where car.Make == "BMW"
&& car.Year == 2010
select car;
//Kedua
var newCars = from car in myCars
where car.Year > 2009
select new
{
car.Make,
car.Model,
car.Year
};
//ketiga
var orderedCars = from car in myCars
orderby car.Year descending
select car;
//keempat
var _bmws = myCars.Where(car => car.Year == 2010).Where(car => car.Make ==
"BMW");

//kelima
var _orderdCars = myCars.OrderByDescending(p => p.Year);
foreach (var Car in _orderdCars)
{
Console.WriteLine("{0} {1} - {2}", Car.Make, Car.Model, Car.Year);
}
//keenam
var sum = myCars.Sum(p => p.StickerPrice);
Console.WriteLine("Total Inventory Value :{0:C}", sum);
Console.ReadLine();
}
}
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public double StickerPrice { get; set; }
public int Year { get; set; }
public CarColor Color { get; set; }
}
enum CarColor
{
White,
Black,
Red,
Blue,
Yellow
}
}
22.

Вам также может понравиться