summaryrefslogtreecommitdiffhomepage
path: root/csharp.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'csharp.html.markdown')
-rw-r--r--csharp.html.markdown34
1 files changed, 23 insertions, 11 deletions
diff --git a/csharp.html.markdown b/csharp.html.markdown
index dad0c26b..4fa8deba 100644
--- a/csharp.html.markdown
+++ b/csharp.html.markdown
@@ -30,6 +30,7 @@ using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Threading.Tasks;
+using System.IO;
// defines scope to organize code into "packages"
namespace Learning
@@ -105,7 +106,7 @@ namespace Learning
Console.WriteLine(fooString);
// You can access each character of the string with an indexer:
- char charFromString = fooString[1]; // 'y'
+ char charFromString = fooString[1]; // => 'e'
// Strings are immutable: you can't do fooString[1] = 'X';
// Compare strings with current culture, ignoring case
@@ -173,7 +174,7 @@ on a new line! ""Wow!"", the masses cried";
int i1 = 1, i2 = 2; // Shorthand for multiple declarations
// Arithmetic is straightforward
- Console.WriteLine(i1 + i2 - i1 * 3 / 7); //
+ Console.WriteLine(i1 + i2 - i1 * 3 / 7); // => 3
// Modulo
Console.WriteLine("11%3 = " + (11 % 3)); // => 2
@@ -241,7 +242,7 @@ on a new line! ""Wow!"", the masses cried";
int fooDoWhile = 0;
do
{
- //Iterated 99 times, fooDoWhile 0->99
+ //Iterated 100 times, fooDoWhile 0->99
fooDoWhile++;
} while (fooDoWhile < 100);
@@ -306,7 +307,7 @@ on a new line! ""Wow!"", the masses cried";
// try parse will default to type default on failure
// in this case: 0
int tryInt;
- if (int.TryParse("123", out tryInt)) // Funciton is boolean
+ if (int.TryParse("123", out tryInt)) // Function is boolean
Console.WriteLine(tryInt); // 123
// Convert Integer To String
@@ -434,6 +435,17 @@ on a new line! ""Wow!"", the masses cried";
Func<int, int> square = (x) => x * x; // Last T item is the return value
Console.WriteLine(square(3)); // 9
+ // DISPOSABLE RESOURCES MANAGEMENT - let you handle unmanaged resources easily.
+ // Most of objects that access unmanaged resources (file handle, device contexts, etc.)
+ // implement the IDisposable interface. The using statement takes care of
+ // cleaning those IDisposable objects for you.
+ using (StreamWriter writer = new StreamWriter("log.txt"))
+ {
+ writer.WriteLine("Nothing suspicious here");
+ // At the end of scope, resources will be released.
+ // Even if an exception is thrown.
+ }
+
// PARALLEL FRAMEWORK
// http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx
var websites = new string[] {
@@ -499,11 +511,11 @@ on a new line! ""Wow!"", the masses cried";
var db = new BikeRespository();
// execution is delayed, which is great when querying a database
- var fitler = db.Bikes.Where(b => b.HasTassles); // no query run
+ var filter = db.Bikes.Where(b => b.HasTassles); // no query run
if (42 > 6) // You can keep adding filters, even conditionally - great for "advanced search" functionality
- fitler = fitler.Where(b => b.IsBroken); // no query run
+ filter = filter.Where(b => b.IsBroken); // no query run
- var query = fitler
+ var query = filter
.OrderBy(b => b.Wheels)
.ThenBy(b => b.Name)
.Select(b => b.Name); // still no query run
@@ -546,7 +558,7 @@ on a new line! ""Wow!"", the masses cried";
}
set // set - define a method to set a proprety
{
- _cadence = value; // Value is the value passed in to to the setter
+ _cadence = value; // Value is the value passed in to the setter
}
}
private int _cadence;
@@ -564,7 +576,7 @@ on a new line! ""Wow!"", the masses cried";
}
int _speed; // Everything is private by default: Only accessible from within this class.
- // can also use keyword privatee
+ // can also use keyword private
public string Name { get; set; }
// Enum is a value type that consists of a set of named constants
@@ -576,7 +588,7 @@ on a new line! ""Wow!"", the masses cried";
AIST,
BMC,
Electra = 42, //you can explicitly set a value to a name
- Gitane
+ Gitane // 43
}
// We defined this type inside a Bicycle class, so it is a nested type
// Code outside of this class should reference this type as Bicycle.Brand
@@ -596,7 +608,7 @@ on a new line! ""Wow!"", the masses cried";
// This is a default constructor
public Bicycle()
{
- this.Gear = 1; // you can access mmebers of the object with the keyword this
+ this.Gear = 1; // you can access members of the object with the keyword this
Cadence = 50; // but you don't always need it
_speed = 5;
Name = "Bontrager";