Learn how events are utilized in the .NET Framework Class Library specific to WPF and ASP.NET Web Forms applications. In these examples, we see how C# is generated by the IDE to "wire up" a user action or application event to the code that handles that event.
WPF
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFEvents
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MyButton.Click += MyButton_Click;
MyButton.Click += MyButton_IClickedThis;
}
void MyButton_Click(object sender, RoutedEventArgs e)
{
//throw new NotImplementedException();
MyLabel.Content = "Hello World!";
}
void MyButton_IClickedThis(object sender, RoutedEventArgs e)
{
//throw new NotImplementedException();
MyLabel.Content = "Hello World again!";
}
}
}
ASP.NET
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ASPNETEvents
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MyButton.Click += MyButton_Click;
}
void MyButton_Click(object sender, EventArgs e)
{
//throw new NotImplementedException();
MyLabel.Text = "Hello world!";
}
}
}
Source : MS Virtual Academy
24 December 2011
23 December 2011
C# Lesson 23 : Filtering and Managing Data Collections Using LINQ
In this lesson, we discuss how Structured Query Language provides a means of working with sets of data. Similarly, the LINQ syntax provides a simple way of working with groups of data in generic collections. We demonstrate projecting data onto existing types and new anonymous types.
Before
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnderstandingLINQ
{
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=25000, Year=2008},
new Car() {Make="BMW", Model="55i", Color=CarColor.Black, StickerPrice=57000, Year=2010}
};
foreach (var car in myCars)
Console.WriteLine("{0} - {1} - {2}", car.Make, car.Model, car.Year);
Console.ReadLine();
}
}
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public double StickerPrice { get; set; }
public CarColor Color { get; set; }
}
enum CarColor
{
White,
Black,
Red,
Blue,
Yellow
}
}
After
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnderstandingLINQ
{
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=25000, Year=2008},
new Car() {Make="BMW", Model="55i", Color=CarColor.Black, StickerPrice=57000, Year=2010}
};
/*
var bmws = from car in myCars
where car.Make == "BMW"
&& car.Year == 2010
select new { car.Make, car.Model, car.Year };
*/
/*
var orderedCars = from car in myCars
orderby car.Year descending
select car;
*/
//var _bmws = myCars.Where(p => p.Year == 2010).Where(p => p.Make == "BMW");
//var _orderedCars = myCars.OrderByDescending(p => p.Year);
var sum = myCars.Sum(p => p.StickerPrice);
/*
foreach (var car in _orderedCars)
Console.WriteLine("{0} - {1} - {2}", car.Make, car.Model, car.Year);
*/
Console.WriteLine(sum);
Console.ReadLine();
}
}
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public double StickerPrice { get; set; }
public CarColor Color { get; set; }
}
enum CarColor
{
White,
Black,
Red,
Blue,
Yellow
}
}
Source : MS Virtual Academy
Before
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnderstandingLINQ
{
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=25000, Year=2008},
new Car() {Make="BMW", Model="55i", Color=CarColor.Black, StickerPrice=57000, Year=2010}
};
foreach (var car in myCars)
Console.WriteLine("{0} - {1} - {2}", car.Make, car.Model, car.Year);
Console.ReadLine();
}
}
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public double StickerPrice { get; set; }
public CarColor Color { get; set; }
}
enum CarColor
{
White,
Black,
Red,
Blue,
Yellow
}
}
After
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnderstandingLINQ
{
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=25000, Year=2008},
new Car() {Make="BMW", Model="55i", Color=CarColor.Black, StickerPrice=57000, Year=2010}
};
/*
var bmws = from car in myCars
where car.Make == "BMW"
&& car.Year == 2010
select new { car.Make, car.Model, car.Year };
*/
/*
var orderedCars = from car in myCars
orderby car.Year descending
select car;
*/
//var _bmws = myCars.Where(p => p.Year == 2010).Where(p => p.Make == "BMW");
//var _orderedCars = myCars.OrderByDescending(p => p.Year);
var sum = myCars.Sum(p => p.StickerPrice);
/*
foreach (var car in _orderedCars)
Console.WriteLine("{0} - {1} - {2}", car.Make, car.Model, car.Year);
*/
Console.WriteLine(sum);
Console.ReadLine();
}
}
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public double StickerPrice { get; set; }
public CarColor Color { get; set; }
}
enum CarColor
{
White,
Black,
Red,
Blue,
Yellow
}
}
Source : MS Virtual Academy
22 December 2011
C# Lesson 22 : Working with Collections
Collections are a more powerful form of arrays. In this lesson, we demonstrate an "old style" collection (pointing out its limitations), along with several of the newer, strongly typed generic collections (List<T> and Dictionary<T1, T2>) utilizing the generics syntax.
Before
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorkingWithCollections
{
class Program
{
static void Main(string[] args)
{
Car car1 = new Car();
car1.Make = "Oldsmobile";
car1.Model = "Cutlas Supreme";
Car car2 = new Car();
car2.Make = "Geo";
car2.Model = "Prism";
Book b1 = new Book();
b1.Author = "Robert Tabor";
b1.Author = "Microsoft .NET XML Web Services";
b1.ISBN = "0-000-00000-0";
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; }
}
}
After
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorkingWithCollections
{
class Program
{
static void Main(string[] args)
{
/*
Car car1 = new Car();
car1.Make = "Oldsmobile";
car1.Model = "Cutlas Supreme";
Car car2 = new Car();
car2.Make = "Geo";
car2.Model = "Prism";
Book b1 = new Book();
b1.Author = "Robert Tabor";
b1.Author = "Microsoft .NET XML Web Services";
b1.ISBN = "0-000-00000-0";
*/
// ArrayLists are dynamically sized, and support other
// cool features like sorting, removing items, etc.
/*
System.Collections.ArrayList myArrayList = new System.Collections.ArrayList();
myArrayList.Add(car1);
myArrayList.Add(car2);
myArrayList.Add(b1);
myArrayList.Remove(b1);
foreach (object o in myArrayList)
{
Console.WriteLine(((Car)o).Make);
}
*/
// Dictionaries allow you to save a key along with
// the value, and also support cool features.
// There are different dictionaries to choose from ...
/*
System.Collections.Specialized.ListDictionary myDictionary
= new System.Collections.Specialized.ListDictionary();
myDictionary.Add(car1.Make, car1);
myDictionary.Add(car2.Make, car2);
myDictionary.Add(b1.Author, b1);
// Easy access to 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 ...
// Obviously, I'm trying to retrieve a book here, and then get its ... model?
Console.WriteLine(((Car)myDictionary["Robert Tabor"]).Model);
*/
/*
List<Car> myList = new List<Car>();
myList.Add(car1);
myList.Add(car2);
//myList.Add(b1);
foreach (Car car in myList)
{
// No casting!
Console.WriteLine(car.Model);
}
*/
/*
Dictionary<string, Car> myDictionary = new Dictionary<string, Car>();
myDictionary.Add(car1.Make, car1);
myDictionary.Add(car2.Make, car2);
Console.WriteLine(myDictionary["Geo"].Model);
*/
//string[] names = { "Bob", "Steve", "Brian", "Chuck" };
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" };
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; }
}
}
Source : MS Virtual Academy
Before
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorkingWithCollections
{
class Program
{
static void Main(string[] args)
{
Car car1 = new Car();
car1.Make = "Oldsmobile";
car1.Model = "Cutlas Supreme";
Car car2 = new Car();
car2.Make = "Geo";
car2.Model = "Prism";
Book b1 = new Book();
b1.Author = "Robert Tabor";
b1.Author = "Microsoft .NET XML Web Services";
b1.ISBN = "0-000-00000-0";
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; }
}
}
After
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorkingWithCollections
{
class Program
{
static void Main(string[] args)
{
/*
Car car1 = new Car();
car1.Make = "Oldsmobile";
car1.Model = "Cutlas Supreme";
Car car2 = new Car();
car2.Make = "Geo";
car2.Model = "Prism";
Book b1 = new Book();
b1.Author = "Robert Tabor";
b1.Author = "Microsoft .NET XML Web Services";
b1.ISBN = "0-000-00000-0";
*/
// ArrayLists are dynamically sized, and support other
// cool features like sorting, removing items, etc.
/*
System.Collections.ArrayList myArrayList = new System.Collections.ArrayList();
myArrayList.Add(car1);
myArrayList.Add(car2);
myArrayList.Add(b1);
myArrayList.Remove(b1);
foreach (object o in myArrayList)
{
Console.WriteLine(((Car)o).Make);
}
*/
// Dictionaries allow you to save a key along with
// the value, and also support cool features.
// There are different dictionaries to choose from ...
/*
System.Collections.Specialized.ListDictionary myDictionary
= new System.Collections.Specialized.ListDictionary();
myDictionary.Add(car1.Make, car1);
myDictionary.Add(car2.Make, car2);
myDictionary.Add(b1.Author, b1);
// Easy access to 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 ...
// Obviously, I'm trying to retrieve a book here, and then get its ... model?
Console.WriteLine(((Car)myDictionary["Robert Tabor"]).Model);
*/
/*
List<Car> myList = new List<Car>();
myList.Add(car1);
myList.Add(car2);
//myList.Add(b1);
foreach (Car car in myList)
{
// No casting!
Console.WriteLine(car.Model);
}
*/
/*
Dictionary<string, Car> myDictionary = new Dictionary<string, Car>();
myDictionary.Add(car1.Make, car1);
myDictionary.Add(car2.Make, car2);
Console.WriteLine(myDictionary["Geo"].Model);
*/
//string[] names = { "Bob", "Steve", "Brian", "Chuck" };
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" };
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; }
}
}
Source : MS Virtual Academy
21 December 2011
C# Lesson 21 : Gracefully Handling Exceptions
Exceptions occur when an application experiences some unexpected problem at run time. This lesson discusses how to use the try catch finally block to anticipate potential problems and to attempt to shield the end user from those problems as much as possible. We also explore best practices when checking for exceptions.
Before
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReadTextFileWhile
{
class Program
{
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();
}
}
}
After
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReadTextFileWhile
{
class Program
{
static void Main(string[] args)
{
try
{
StreamReader myReader = new StreamReader("Values.txt");
string line = "";
while (line != null)
{
line = myReader.ReadLine();
if (line != null)
Console.WriteLine(line);
}
myReader.Close();
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("Couldn't fine the file. Are you sure the DIRECTORY exists?");
}
catch (FileNotFoundException e)
{
Console.WriteLine("Couldn't find the file. Are you sure you're looking for the correct file?");
}
catch (Exception e)
{
Console.WriteLine("Something didn't quite work correctly: {0}", e.Message);
}
finally
{
// Perform any cleanup to roll back the data or close connections
// to files, database, network, etc.
}
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
Before
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReadTextFileWhile
{
class Program
{
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();
}
}
}
After
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReadTextFileWhile
{
class Program
{
static void Main(string[] args)
{
try
{
StreamReader myReader = new StreamReader("Values.txt");
string line = "";
while (line != null)
{
line = myReader.ReadLine();
if (line != null)
Console.WriteLine(line);
}
myReader.Close();
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("Couldn't fine the file. Are you sure the DIRECTORY exists?");
}
catch (FileNotFoundException e)
{
Console.WriteLine("Couldn't find the file. Are you sure you're looking for the correct file?");
}
catch (Exception e)
{
Console.WriteLine("Something didn't quite work correctly: {0}", e.Message);
}
finally
{
// Perform any cleanup to roll back the data or close connections
// to files, database, network, etc.
}
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
20 December 2011
C# Lesson 20 : Enumerations and the switch Decision Statement
Here, we demonstrate the use of Enumerations because, in the .NET Framework Class Library, properties can often be set only to a predetermined subset of possible values. To illustrate this point, we create our own custom enumeration and then utilize it in a simple application that demonstrates a third Decision statement, the switch.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UsingSwitch
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Type in a super hero's name to see his nickname:");
string userValue = Console.ReadLine();
switch(userValue.ToUpper())
{
case "BATMAN":
Console.WriteLine("Caped Crusader");
break;
case "SUPERMAN":
Console.WriteLine("Man of Steel");
break;
case "GREENLANTERN":
Console.WriteLine("Emerald Knight");
break;
default:
Console.WriteLine("Does not compute");
break;
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnderstandingEnumerations
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
//Console.WriteLine("Hello world!");
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("Caped crusader");
break;
case SuperHero.Superman:
Console.WriteLine("Man of Steel");
break;
case SuperHero.GreenLantern:
Console.WriteLine("Emerald Knight");
break;
default:
break;
}
}
else
{
Console.WriteLine("Does not compute");
}
Console.ReadLine();
}
}
enum SuperHero
{
Batman,
Superman,
GreenLantern
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComplexIfStatement
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Pick a number between 1 and 100:");
string userValue = Console.ReadLine();
int compareValue = int.Parse(userValue);
if ((compareValue < 1) || (compareValue > 100))
Console.WriteLine("The number you chose was out of bounds.");
else if ((compareValue == 42) || (compareValue > 90))
Console.WriteLine("You found one of the special numbers!");
else
Console.WriteLine("You didn't find one of the special numbers");
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UsingSwitch
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Type in a super hero's name to see his nickname:");
string userValue = Console.ReadLine();
switch(userValue.ToUpper())
{
case "BATMAN":
Console.WriteLine("Caped Crusader");
break;
case "SUPERMAN":
Console.WriteLine("Man of Steel");
break;
case "GREENLANTERN":
Console.WriteLine("Emerald Knight");
break;
default:
Console.WriteLine("Does not compute");
break;
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnderstandingEnumerations
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
//Console.WriteLine("Hello world!");
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("Caped crusader");
break;
case SuperHero.Superman:
Console.WriteLine("Man of Steel");
break;
case SuperHero.GreenLantern:
Console.WriteLine("Emerald Knight");
break;
default:
break;
}
}
else
{
Console.WriteLine("Does not compute");
}
Console.ReadLine();
}
}
enum SuperHero
{
Batman,
Superman,
GreenLantern
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComplexIfStatement
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Pick a number between 1 and 100:");
string userValue = Console.ReadLine();
int compareValue = int.Parse(userValue);
if ((compareValue < 1) || (compareValue > 100))
Console.WriteLine("The number you chose was out of bounds.");
else if ((compareValue == 42) || (compareValue > 90))
Console.WriteLine("You found one of the special numbers!");
else
Console.WriteLine("You didn't find one of the special numbers");
Console.ReadLine();
}
}
}
19 December 2011
C# Lesson 19 : Understanding Scope and Utilizing Accessibility Modifiers
Explore the scope of variables within code blocks and how accessibility modifiers, such as Public, Private, and Protected, are used by the .NET Framework Class Library to expose or hide implementation of their given services to consumers of that given class. This is sometimes referred to as "encapsulation."
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnderstandingScope
{
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: " + l);
}
//Console.WriteLine(i);
Console.WriteLine("Outsite of the for: " + j);
//Console.WriteLine("k: " + k);
helperMethod();
*/
Car car = new Car();
car.DoSomething();
Console.ReadLine();
}
/*
static void helperMethod()
{
Console.WriteLine("k from the helperMethod: " + k);
}
*/
}
class Car
{
public void DoSomething()
{
Console.WriteLine(helperMethod());
}
private string helperMethod()
{
return "Hello world!";
}
}
}
Source : MS Virtual Academy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnderstandingScope
{
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: " + l);
}
//Console.WriteLine(i);
Console.WriteLine("Outsite of the for: " + j);
//Console.WriteLine("k: " + k);
helperMethod();
*/
Car car = new Car();
car.DoSomething();
Console.ReadLine();
}
/*
static void helperMethod()
{
Console.WriteLine("k from the helperMethod: " + k);
}
*/
}
class Car
{
public void DoSomething()
{
Console.WriteLine(helperMethod());
}
private string helperMethod()
{
return "Hello world!";
}
}
}
Source : MS Virtual Academy
18 December 2011
C# Lesson 18 : Understanding Namespaces and Adding References to Assemblies
In this lesson, we explain how Namespaces allow us to disambiguate classes that may share the same name. And we explain how the .NET Framework Class Library is so large that including all its classes in every application you write is a waste of system resources. Certain project templates include references to the typical assemblies required by a given type of application, and we demonstrate this by referencing a custom assembly of Bob's own design.
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tabor;
namespace NamespacesAndReferencingAssemblies
{
class Program
{
static void Main(string[] args)
{
//System.IO.StreamReader myStreamReader = new System.IO.StreamReader();
//StreamReader myStreamReader = new StreamReader();
Bob bob = new Bob();
string html = bob.Lookup("http://www.learnvisualstudio.net");
Console.WriteLine(html);
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tabor;
namespace NamespacesAndReferencingAssemblies
{
class Program
{
static void Main(string[] args)
{
//System.IO.StreamReader myStreamReader = new System.IO.StreamReader();
//StreamReader myStreamReader = new StreamReader();
Bob bob = new Bob();
string html = bob.Lookup("http://www.learnvisualstudio.net");
Console.WriteLine(html);
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
17 December 2011
C# Lesson 17 : Working with Classes and Inheritances in the .NET Framework Class Library
This lesson continues to teach concepts about classes (specifically, in this case, inheritance) by showing you how to utilize inheritance in your own custom classes. Learn about overriding virtual functionality, abstract base classes, and sealed classes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnderstandingInheritance
{
class Program
{
static void Main(string[] args)
{
Car myCar = new Car();
myCar.Make = "BMW";
myCar.Model = "745li";
myCar.Color = "Black";
myCar.Year = 2005;
//myCar.TowingCapacity = 1200;
printVehicleDetails(myCar);
Truck myTruck = new Truck();
myTruck.Make = "Ford";
myTruck.Model = "F950";
myTruck.Year = 2006;
myTruck.Color = "Black";
myTruck.TowingCapacity = 1200;
printVehicleDetails(myTruck);
Console.ReadLine();
}
private static void printVehicleDetails(Vehicle vehicle)
{
Console.WriteLine("Here are the vehcile's details: {0}",
vehicle.FormatMe());
}
}
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();
}
class Car : Vehicle
{
public override string FormatMe()
{
return String.Format("{0} - {1} - {2} - {3}",
this.Make,
this.Model,
this.Color,
this.Year);
}
}
sealed class Truck : Vehicle
{
public int TowingCapacity { get; set; }
public override string FormatMe()
{
return String.Format("{0} - {1} - {2} Towing units",
this.Make,
this.Model,
this.TowingCapacity);
}
}
/*
class Semi : Truck
{
}
*/
}
Source : MS Virtual Academy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnderstandingInheritance
{
class Program
{
static void Main(string[] args)
{
Car myCar = new Car();
myCar.Make = "BMW";
myCar.Model = "745li";
myCar.Color = "Black";
myCar.Year = 2005;
//myCar.TowingCapacity = 1200;
printVehicleDetails(myCar);
Truck myTruck = new Truck();
myTruck.Make = "Ford";
myTruck.Model = "F950";
myTruck.Year = 2006;
myTruck.Color = "Black";
myTruck.TowingCapacity = 1200;
printVehicleDetails(myTruck);
Console.ReadLine();
}
private static void printVehicleDetails(Vehicle vehicle)
{
Console.WriteLine("Here are the vehcile's details: {0}",
vehicle.FormatMe());
}
}
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();
}
class Car : Vehicle
{
public override string FormatMe()
{
return String.Format("{0} - {1} - {2} - {3}",
this.Make,
this.Model,
this.Color,
this.Year);
}
}
sealed class Truck : Vehicle
{
public int TowingCapacity { get; set; }
public override string FormatMe()
{
return String.Format("{0} - {1} - {2} Towing units",
this.Make,
this.Model,
this.TowingCapacity);
}
}
/*
class Semi : Truck
{
}
*/
}
Source : MS Virtual Academy
16 December 2011
C# Lesson 16 : More About Classes and Methods
This lesson digs into more details about classes—what exactly happens when you create a new instance of a class? What is a reference to an instance of a class? How does passing the reference to a method affect a class? We also review overloaded methods, static versus instance methods, and constructors.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ObjectLifetime
{
class Program
{
static void Main(string[] args)
{
Car myCar = new Car();
// set properties
Car myOtherCar = myCar;
Car myThirdCar = new Car("Ford", "Escape", 2005, "White");
myOtherCar = null;
myCar = null;
}
}
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public string Color { get; set; }
public double OriginalPrice { get; set; }
public Car()
{
// You could this from a configuration file, a database, etc.
// I'll just hardcode in this instance.
this.Make = "Nissan";
}
public Car(string make, string model, int year, string color)
{
Make = make;
Model = model;
Year = year;
Color = color;
}
/*
public Car(string someOtherInputParameter, string model, int year, string color)
{
Make = someOtherInputParameter;
Model = model;
Year = year;
Color = color;
}
* */
public static void MyMethod()
{
}
}
}
Source : MS Virtual Academy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ObjectLifetime
{
class Program
{
static void Main(string[] args)
{
Car myCar = new Car();
// set properties
Car myOtherCar = myCar;
Car myThirdCar = new Car("Ford", "Escape", 2005, "White");
myOtherCar = null;
myCar = null;
}
}
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public string Color { get; set; }
public double OriginalPrice { get; set; }
public Car()
{
// You could this from a configuration file, a database, etc.
// I'll just hardcode in this instance.
this.Make = "Nissan";
}
public Car(string make, string model, int year, string color)
{
Make = make;
Model = model;
Year = year;
Color = color;
}
/*
public Car(string someOtherInputParameter, string model, int year, string color)
{
Make = someOtherInputParameter;
Model = model;
Year = year;
Color = color;
}
* */
public static void MyMethod()
{
}
}
}
Source : MS Virtual Academy
15 December 2011
C# Lesson 15 : Understanding and Creating Classes
Classes are integral to the .NET Framework, particularly the .NET Framework Class Library. Learn how classes are defined and new instances are created, how to define Properties, and how to both set values and get values for a given instance of the class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleClasses
{
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);
//double marketValueOfCar = determineMarketValue(myNewCar);
Console.WriteLine("Car's value: {0:C}", myNewCar.DetermineMarketValue());
Console.ReadLine();
}
private static double determineMarketValue(Car _car)
{
double carValue = 100.0;
// Somedat white come to go online and look up the car's value
// and trieve its value into the carValue variable
return carValue;
}
}
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public string Color { get; set; }
public double DetermineMarketValue()
{
double carValue = 100.0;
if (this.Year > 1990)
carValue = 10000.0;
else
carValue = 2000.0;
return carValue;
}
}
}
Source : MS Virtual Academy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleClasses
{
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);
//double marketValueOfCar = determineMarketValue(myNewCar);
Console.WriteLine("Car's value: {0:C}", myNewCar.DetermineMarketValue());
Console.ReadLine();
}
private static double determineMarketValue(Car _car)
{
double carValue = 100.0;
// Somedat white come to go online and look up the car's value
// and trieve its value into the carValue variable
return carValue;
}
}
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public string Color { get; set; }
public double DetermineMarketValue()
{
double carValue = 100.0;
if (this.Year > 1990)
carValue = 10000.0;
else
carValue = 2000.0;
return carValue;
}
}
}
Source : MS Virtual Academy
14 December 2011
C# Lesson 14 : Working with DateTime
Like strings, dates and times are represented using special types and so deserve some attention. In this lesson, we learn how to work with Date and Time data, how to create new instances of DateTime, how to add time, and how to format the data for display. We also discuss the TimeSpan class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatesAndTimes
{
class Program
{
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.AddDays(-3).ToShortDateString());
//Console.WriteLine(myValue.Month.ToString());
//DateTime myBirthday = new DateTime(1969, 12, 7);
//Console.WriteLine(myBirthday.ToShortDateString());
DateTime myBirthday = DateTime.Parse("12/7/1969");
TimeSpan myAge = DateTime.Now.Subtract(myBirthday);
Console.WriteLine(myAge.TotalDays);
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatesAndTimes
{
class Program
{
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.AddDays(-3).ToShortDateString());
//Console.WriteLine(myValue.Month.ToString());
//DateTime myBirthday = new DateTime(1969, 12, 7);
//Console.WriteLine(myBirthday.ToShortDateString());
DateTime myBirthday = DateTime.Parse("12/7/1969");
TimeSpan myAge = DateTime.Now.Subtract(myBirthday);
Console.WriteLine(myAge.TotalDays);
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
13 December 2011
C# Lesson 13 : Working with Strings
Since oftentimes in our applications we'll want to work with string data, this lesson approaches a number of different string manipulations. We look at built-in String methods to manipulate the content inside of a literal string and at the StringBuilder class for concatenating many strings together in a memory and resource-friendly manner.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Strings
{
class Program
{
static void Main(string[] args)
{
//string myString = "Go to your c:\\ drive";
//string myString = "My \"so called\" life";
//string myString = "What if I need \n a new line?";
//string myString = string.Format("{0}!", "Bonzai");
//string myString = string.Format("Make: {0} (Model: {1})", "BMW", "760li");
//string myString = string.Format("{0:C}", 123.45);
//string myString = string.Format("{0:N}", 123456789);
//string myString = string.Format("{0:P}", .123);
//string myString = string.Format("Phone number: {0:(###) ###-####}", 1234567890);
/*
string myString = "";
for (int i = 0; i < 100; i++)
{
//myString = myString + "--" + i.ToString();
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();
}
}
}
Source : MS Virtual Academy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Strings
{
class Program
{
static void Main(string[] args)
{
//string myString = "Go to your c:\\ drive";
//string myString = "My \"so called\" life";
//string myString = "What if I need \n a new line?";
//string myString = string.Format("{0}!", "Bonzai");
//string myString = string.Format("Make: {0} (Model: {1})", "BMW", "760li");
//string myString = string.Format("{0:C}", 123.45);
//string myString = string.Format("{0:N}", 123456789);
//string myString = string.Format("{0:P}", .123);
//string myString = string.Format("Phone number: {0:(###) ###-####}", 1234567890);
/*
string myString = "";
for (int i = 0; i < 100; i++)
{
//myString = myString + "--" + i.ToString();
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();
}
}
}
Source : MS Virtual Academy
12 December 2011
C# Lesson 12 : While Iterations and Reading Data from a Text File
Learn a new type of iteration statement (while) and how to utilize the StreamReader class to stream data from a file to the Console window. Additionally, we learn how to add new files to our project, how to set properties of our file using the Properties window, and how to add a using statement as a means of resolving a class name referenced in our code to the namespace in which it is defined.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReadTextFileWhile
{
class Program
{
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();
}
}
}
Source : MS Virtual Academy
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReadTextFileWhile
{
class Program
{
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();
}
}
}
Source : MS Virtual Academy
11 December 2011
C# Lesson 11 : Creating and Calling Simple Overloaded Helper Methods
Now, we begin wading into the topic of methods by creating a helper method to break out code we may need to use in multiple places within our code. We create and call our methods to retrieve a value, create and use input parameters, learn about string formatting, and create overloaded versions of our method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelperMethods
{
class Program
{
static void Main(string[] args)
{
//string myValue = superSecretFormula("world");
string myValue = superSecretFormula("sunshine");
Console.WriteLine(myValue);
Console.ReadLine();
}
private static string superSecretFormula()
{
// some cool stuff here
return "Hello World!";
}
private static string superSecretFormula(string name)
{
return String.Format("Hello, {0}!", name);
}
}
}
Source : MS Virtual Academy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelperMethods
{
class Program
{
static void Main(string[] args)
{
//string myValue = superSecretFormula("world");
string myValue = superSecretFormula("sunshine");
Console.WriteLine(myValue);
Console.ReadLine();
}
private static string superSecretFormula()
{
// some cool stuff here
return "Hello World!";
}
private static string superSecretFormula(string name)
{
return String.Format("Hello, {0}!", name);
}
}
}
Source : MS Virtual Academy
10 December 2011
C# Lesson 10 : Creating Arrays of Values
In this lesson, we talk about arrays, which are multi-part variables—a "bucket" containing other "buckets," if you will. We demonstrate how to declare and utilize arrays, and we demonstrate a couple of powerful built-in methods that give arrays added features.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnderstandingArrays
{
class Program
{
static void Main(string[] args)
{
int[] numbers = new int[5];
numbers[0] = 4;
numbers[1] = 8;
numbers[2] = 15;
numbers[3] = 16;
numbers[4] = 23;
//numbers[5] = 42;
Console.WriteLine(numbers.Length);
/*
int[] numbers = new int[] { 4, 8, 15, 16, 23, 42 };
Console.WriteLine(numbers[1].ToString());
Console.ReadLine();
*/
/*
string[] names = new string[] { "Eddie", "Alex", "Michael", "David Lee" };
foreach (string name in names)
{
Console.WriteLine(name);
}
Console.ReadLine();
*/
/*
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 zigChar in charArray)
Console.Write(zigChar);
*/
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnderstandingArrays
{
class Program
{
static void Main(string[] args)
{
int[] numbers = new int[5];
numbers[0] = 4;
numbers[1] = 8;
numbers[2] = 15;
numbers[3] = 16;
numbers[4] = 23;
//numbers[5] = 42;
Console.WriteLine(numbers.Length);
/*
int[] numbers = new int[] { 4, 8, 15, 16, 23, 42 };
Console.WriteLine(numbers[1].ToString());
Console.ReadLine();
*/
/*
string[] names = new string[] { "Eddie", "Alex", "Michael", "David Lee" };
foreach (string name in names)
{
Console.WriteLine(name);
}
Console.ReadLine();
*/
/*
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 zigChar in charArray)
Console.Write(zigChar);
*/
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
09 December 2011
C# Lesson 9 : For Iterations
Iterations allow our applications to loop through a block of code until a condition is satisfied. We cover several different types of iteration statements throughout this series, how to utilize "code snippets" to help remind you of the syntax for this complex statement, and debugging in action.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ForIterations
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
//Console.WriteLine(i.ToString());
if (i == 7)
{
Console.WriteLine("Found seven!");
break;
}
}
for (int myValue = 0; myValue < 12; myValue++)
{
Console.WriteLine(myValue);
}
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ForIterations
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
//Console.WriteLine(i.ToString());
if (i == 7)
{
Console.WriteLine("Found seven!");
break;
}
}
for (int myValue = 0; myValue < 12; myValue++)
{
Console.WriteLine(myValue);
}
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
08 December 2011
C# Lesson 8 : Operators, Expressions, and Statements Duration
In this lesson, we discuss how to create a properly formed C# statement. We discuss how statements are made up of expressions and how expressions are made up of operators (think: verbs) and operands (think: nouns). Finally, we talk about compilation errors that occur when the syntax rules of C# are ignored.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OperatorsExpressionsStatements
{
class Program
{
static void Main(string[] args)
{
int x, y, a, b;
// Assignment operator
x = 3;
y = 2;
a = 1;
b = 0;
// There are many mathematical operators ...
// Addition operator
x = 3 + 4;
// Subtraction operator
x = 4 - 3;
// Multiplication operator
x = 10 * 5;
// Division operator
x = 10 / 5;
// There are many operators used to evaluate values ...
// Equality operator
if (x == y)
{
}
// Greater than operator
if (x > y)
{
}
// Less than operator
if (x < y)
{
}
// Greater or equal to operator
if (x >= y)
{
}
// Less than or equal to operator
if (x <= y)
{
}
// There are two "conditional" operators as well that can be used to expand / enhance an evaluation ...
// ... and they can be combined together multiple times.
// Conditional AND operator …
if ((x > y) && (a > b))
{
}
// Conditional OR operator …
if ((x > y) || (a > b))
{
}
// Also, here's the in-line conditional operator we learned about in the previous lesson ...
string message = (x == 1) ? "Car" : "Boat";
// Member access and Method invocation
Console.WriteLine("Hi");
x + y;
}
}
}
Source : MS Virtual Academy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OperatorsExpressionsStatements
{
class Program
{
static void Main(string[] args)
{
int x, y, a, b;
// Assignment operator
x = 3;
y = 2;
a = 1;
b = 0;
// There are many mathematical operators ...
// Addition operator
x = 3 + 4;
// Subtraction operator
x = 4 - 3;
// Multiplication operator
x = 10 * 5;
// Division operator
x = 10 / 5;
// There are many operators used to evaluate values ...
// Equality operator
if (x == y)
{
}
// Greater than operator
if (x > y)
{
}
// Less than operator
if (x < y)
{
}
// Greater or equal to operator
if (x >= y)
{
}
// Less than or equal to operator
if (x <= y)
{
}
// There are two "conditional" operators as well that can be used to expand / enhance an evaluation ...
// ... and they can be combined together multiple times.
// Conditional AND operator …
if ((x > y) && (a > b))
{
}
// Conditional OR operator …
if ((x > y) || (a > b))
{
}
// Also, here's the in-line conditional operator we learned about in the previous lesson ...
string message = (x == 1) ? "Car" : "Boat";
// Member access and Method invocation
Console.WriteLine("Hi");
x + y;
}
}
}
Source : MS Virtual Academy
07 December 2011
C# Lesson 7 : Branching with the if Decision Statement and the Conditional Operator
Branching allows us to add logic to our applications. This lesson introduces the ‘if Decision’ statement (in its various forms), along with the conditional operator. We also discuss how to refactor our code to make it more compact and less likely to produce errors, by eliminating duplicate code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Decisions
{
class Program
{
static void Main(string[] args)
{
//Console.WriteLine("Please type something and press the Enter key.");
//string userValue;
//userValue = Console.ReadLine();
//Console.WriteLine("You typed: " + userValue);
Console.WriteLine("Would you prefer what is behind door number 1, 2, or 3?");
string userValue = Console.ReadLine();
//string message = "";
//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 cat!";
//else
// message = "Sorry, we didn't understand. You lose!";
//Console.WriteLine(message);
string message = (userValue == "1") ? "boat" : "strand of lint";
Console.WriteLine("You won a {0}", message);
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Decisions
{
class Program
{
static void Main(string[] args)
{
//Console.WriteLine("Please type something and press the Enter key.");
//string userValue;
//userValue = Console.ReadLine();
//Console.WriteLine("You typed: " + userValue);
Console.WriteLine("Would you prefer what is behind door number 1, 2, or 3?");
string userValue = Console.ReadLine();
//string message = "";
//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 cat!";
//else
// message = "Sorry, we didn't understand. You lose!";
//Console.WriteLine(message);
string message = (userValue == "1") ? "boat" : "strand of lint";
Console.WriteLine("You won a {0}", message);
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
06 December 2011
C# Lesson 6 : Declaring Variables and Assigning Values Duration
Here, we start adding C# syntax to your vocabulary by talking about fundamental building blocks: data types and variables. We also discuss basic topics, such as naming conventions and data type conversions.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Variables
{
class Program
{
static void Main(string[] args)
{
/*
int x;
int y;
x = 7;
y = x + 3;
Console.WriteLine(y);
*/
//string myFirstName;
//myFirstName = "Bob";
//string myFirstNane = "Bob";
//var myFirstName = "Bob";
//var myFirstName = "";
//string myfirstname;
//Console.WriteLine(myFirstName);
int x = 7;
//string y = "Bob";
string y = "5";
string myFirstTry = x.ToString() + y;
//int mySecondTry = x + y;
int mySecondTry = x + int.Parse(y);
Console.WriteLine(myFirstTry);
//Console.WriteLine(mySecondTry);
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Variables
{
class Program
{
static void Main(string[] args)
{
/*
int x;
int y;
x = 7;
y = x + 3;
Console.WriteLine(y);
*/
//string myFirstName;
//myFirstName = "Bob";
//string myFirstNane = "Bob";
//var myFirstName = "Bob";
//var myFirstName = "";
//string myfirstname;
//Console.WriteLine(myFirstName);
int x = 7;
//string y = "Bob";
string y = "5";
string myFirstTry = x.ToString() + y;
//int mySecondTry = x + y;
int mySecondTry = x + int.Parse(y);
Console.WriteLine(myFirstTry);
//Console.WriteLine(mySecondTry);
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
03 December 2011
C# Lesson 3 : Creating Your First C# Program
This lesson teaches you how to create a simple application—first using Windows Notepad and the C# Command Line Compiler, and then by using Visual Studio or Visual C# Express Edition. The video concludes with an explanation of common solutions to the many different problems you might encounter as you first begin writing and compiling code.
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)
{
// This is a comment
Console.WriteLine("Hello World");
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
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)
{
// This is a comment
Console.WriteLine("Hello World");
Console.ReadLine();
}
}
}
Source : MS Virtual Academy
Subscribe to:
Posts (Atom)