summaryrefslogtreecommitdiffhomepage
path: root/csharp.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'csharp.html.markdown')
-rw-r--r--csharp.html.markdown65
1 files changed, 35 insertions, 30 deletions
diff --git a/csharp.html.markdown b/csharp.html.markdown
index 8babb90a..28da9fe5 100644
--- a/csharp.html.markdown
+++ b/csharp.html.markdown
@@ -26,7 +26,7 @@ Multi-line comments look like this
//public void MethodOrClassOrOtherWithParsableHelp() {}
// Specify the namespaces this source code will be using
-// The namespaces below are all part of the standard .NET Framework Class Libary
+// The namespaces below are all part of the standard .NET Framework Class Libary
using System;
using System.Collections.Generic;
using System.Dynamic;
@@ -49,7 +49,7 @@ namespace Learning.CSharp
public class LearnCSharp
{
// BASIC SYNTAX - skip to INTERESTING FEATURES if you have used Java or C++ before
- public static void Syntax()
+ public static void Syntax()
{
// Use Console.WriteLine to print lines
Console.WriteLine("Hello World");
@@ -237,7 +237,8 @@ on a new line! ""Wow!"", the masses cried";
// Ternary operators
// A simple if/else can be written as follows
// <condition> ? <true> : <false>
- string isTrue = (true) ? "True" : "False";
+ int toCompare = 17;
+ string isTrue = toCompare == 17 ? "True" : "False";
// While loop
int fooWhile = 0;
@@ -371,11 +372,11 @@ on a new line! ""Wow!"", the masses cried";
//
// INTERESTING FEATURES
//
-
+
// DEFAULT METHOD SIGNATURES
public // Visibility
- static // Allows for direct call on class without object
+ static // Allows for direct call on class without object
int // Return Type,
MethodSignatures(
int maxCount, // First variable, expects an int
@@ -383,7 +384,7 @@ on a new line! ""Wow!"", the masses cried";
int another = 3,
params string[] otherParams // captures all other parameters passed to method
)
- {
+ {
return -1;
}
@@ -400,8 +401,8 @@ on a new line! ""Wow!"", the masses cried";
// The classes for TKey and TValue is specified by the user calling this function.
// This method emulates the SetDefault of Python
public static TValue SetDefault<TKey, TValue>(
- IDictionary<TKey, TValue> dictionary,
- TKey key,
+ IDictionary<TKey, TValue> dictionary,
+ TKey key,
TValue defaultItem)
{
TValue result;
@@ -410,7 +411,7 @@ on a new line! ""Wow!"", the masses cried";
return result;
}
- // You can narrow down the objects that are passed in
+ // You can narrow down the objects that are passed in
public static void IterateAndPrint<T>(T toPrint) where T: IEnumerable<int>
{
// We can iterate, since T is a IEnumerable
@@ -479,6 +480,9 @@ on a new line! ""Wow!"", the masses cried";
// ?? is syntactic sugar for specifying default value (coalesce)
// in case variable is null
int notNullable = nullable ?? 0; // 0
+
+ // ?. is an operator for null-propogation - a shorthand way of checking for null
+ nullable?.Print(); // Use the Print() extension method if nullable isn't null
// IMPLICITLY TYPED VARIABLES - you can let the compiler work out what the type is:
var magic = "magic is a string, at compile time, so you still get type safety";
@@ -486,13 +490,13 @@ on a new line! ""Wow!"", the masses cried";
// GENERICS
//
- var phonebook = new Dictionary<string, string>() {
+ var phonebook = new Dictionary<string, string>() {
{"Sarah", "212 555 5555"} // Add some entries to the phone book
};
// Calling SETDEFAULT defined as a generic above
Console.WriteLine(SetDefault<string,string>(phonebook, "Shaun", "No Phone")); // No Phone
- // nb, you don't need to specify the TKey and TValue since they can be
+ // nb, you don't need to specify the TKey and TValue since they can be
// derived implicitly
Console.WriteLine(SetDefault(phonebook, "Sarah", "No Phone")); // 212 555 5555
@@ -527,26 +531,26 @@ on a new line! ""Wow!"", the masses cried";
// 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
+ // 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[] {
- "http://www.google.com", "http://www.reddit.com",
+ var websites = new string[] {
+ "http://www.google.com", "http://www.reddit.com",
"http://www.shaunmccarthy.com"
};
var responses = new Dictionary<string, string>();
-
+
// Will spin up separate threads for each request, and join on them
// before going to the next step!
- Parallel.ForEach(websites,
+ Parallel.ForEach(websites,
new ParallelOptions() {MaxDegreeOfParallelism = 3}, // max of 3 threads
website =>
{
@@ -570,7 +574,7 @@ on a new line! ""Wow!"", the masses cried";
(introduceTo) => string.Format("Hey {0}, this is {1}", student.FirstName, introduceTo));
Console.WriteLine(student.Introduce("Beth"));
- // IQUERYABLE<T> - almost all collections implement this, which gives you a lot of
+ // IQUERYABLE<T> - almost all collections implement this, which gives you a lot of
// very useful Map / Filter / Reduce style methods
var bikes = new List<Bicycle>();
bikes.Sort(); // Sorts the array
@@ -592,8 +596,8 @@ on a new line! ""Wow!"", the masses cried";
// ASPARALLEL
// And this is where things get wicked - combines linq and parallel operations
var threeWheelers = bikes.AsParallel().Where(b => b.Wheels == 3).Select(b => b.Name);
- // this will happen in parallel! Threads will automagically be spun up and the
- // results divvied amongst them! Amazing for large datasets when you have lots of
+ // this will happen in parallel! Threads will automagically be spun up and the
+ // results divvied amongst them! Amazing for large datasets when you have lots of
// cores
// LINQ - maps a store to IQueryable<T> objects, with delayed execution
@@ -611,9 +615,9 @@ on a new line! ""Wow!"", the masses cried";
.Select(b => b.Name); // still no query run
// Now the query runs, but opens a reader, so only populates are you iterate through
- foreach (string bike in query)
+ foreach (string bike in query)
Console.WriteLine(result);
-
+
}
@@ -665,7 +669,7 @@ on a new line! ""Wow!"", the masses cried";
private set; // You can set modifiers on the get/set methods
}
- int _speed; // Everything is private by default: Only accessible from within this class.
+ int _speed; // Everything is private by default: Only accessible from within this class.
// can also use keyword private
public string Name { get; set; }
@@ -712,7 +716,7 @@ on a new line! ""Wow!"", the masses cried";
// Constructors are a way of creating classes
// This is a default constructor
- public Bicycle()
+ public Bicycle()
{
this.Gear = 1; // you can access members of the object with the keyword this
Cadence = 50; // but you don't always need it
@@ -724,13 +728,13 @@ on a new line! ""Wow!"", the masses cried";
// This is a specified constructor (it contains arguments)
public Bicycle(int startCadence, int startSpeed, int startGear,
- string name, bool hasCardsInSpokes, BikeBrand brand)
+ string name, bool hasCardsInSpokes, BikeBrand brand)
: base() // calls base first
{
- Gear = startGear;
+ Gear = startGear;
Cadence = startCadence;
_speed = startSpeed;
- Name = name;
+ Name = name;
_hasCardsInSpokes = hasCardsInSpokes;
Brand = brand;
}
@@ -873,7 +877,8 @@ on a new line! ""Wow!"", the masses cried";
bool Broken { get; } // interfaces can contain properties as well as methods & events
}
- // Class can inherit only one other class, but can implement any amount of interfaces
+ // Class can inherit only one other class, but can implement any amount of interfaces, however
+ // the base class name must be the first in the list and all interfaces follow
class MountainBike : Bicycle, IJumpable, IBreakable
{
int damage = 0;
@@ -893,7 +898,7 @@ on a new line! ""Wow!"", the masses cried";
}
/// <summary>
- /// Used to connect to DB for LinqToSql example.
+ /// Used to connect to DB for LinqToSql example.
/// EntityFramework Code First is awesome (similar to Ruby's ActiveRecord, but bidirectional)
/// http://msdn.microsoft.com/en-us/data/jj193542.aspx
/// </summary>
@@ -918,7 +923,7 @@ on a new line! ""Wow!"", the masses cried";
* ASP.NET Web Forms (old)
* WebMatrix (tool)
* Desktop Development
- * Windows Presentation Foundation (WPF) (new)
+ * Windows Presentation Foundation (WPF) (new)
* Winforms (old)
## Further Reading