summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--csharp.html.markdown43
1 files changed, 35 insertions, 8 deletions
diff --git a/csharp.html.markdown b/csharp.html.markdown
index 47dd9683..8eda5356 100644
--- a/csharp.html.markdown
+++ b/csharp.html.markdown
@@ -18,13 +18,15 @@ C# is an elegant and type-safe object-oriented language that enables developers
Multi-line comments look like this
*/
/// <summary>
-/// This is an XML documentation comment
+/// This is an XML documentation comment which can be used to generate external
+/// documentation or provide context help within an IDE
/// </summary>
+//public void MethodOrClassOrOtherWithParsableHelp() {}
// Specify namespaces application will be using
using System;
using System.Collections.Generic;
-using System.Data.Entity;
+using System.Data.Entity; // Add dll reference with NuGet: Install-Package EntityFramework
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
@@ -33,7 +35,7 @@ using System.Threading.Tasks;
using System.IO;
// defines scope to organize code into "packages"
-namespace Learning
+namespace LearningXInYMinutes.CSharp
{
// Each .cs file should at least contain a class with the same name as the file
// you're allowed to do otherwise, but shouldn't for sanity.
@@ -125,7 +127,7 @@ on a new line! ""Wow!"", the masses cried";
// Use const or read-only to make a variable immutable
// const values are calculated at compile time
- const int HOURS_I_WORK_PER_WEEK = 9001;
+ const int HoursWorkPerWeek = 9001;
///////////////////////////////////////////////////
// Data Structures
@@ -242,8 +244,15 @@ on a new line! ""Wow!"", the masses cried";
int fooDoWhile = 0;
do
{
- //Iterated 100 times, fooDoWhile 0->99
+ // Start iteration 100 times, fooDoWhile 0->99
+ if (false)
+ continue; // skip the current iteration
+
fooDoWhile++;
+
+ if (fooDoWhile == 50)
+ break; // breaks from the loop completely
+
} while (fooDoWhile < 100);
//for loop structure => for(<start_statement>; <conditional>; <step>)
@@ -301,7 +310,7 @@ on a new line! ""Wow!"", the masses cried";
// Converting data
// Convert String To Integer
- // this will throw an Exception on failure
+ // this will throw a FormatException on failure
int.Parse("123");//returns an integer version of "123"
// try parse will default to type default on failure
@@ -315,6 +324,11 @@ on a new line! ""Wow!"", the masses cried";
Convert.ToString(123);
// or
tryInt.ToString();
+
+ // Casting
+ // Cast decimal 15 to a int
+ // and then implicitly cast to long
+ long x = (int) 15M;
}
///////////////////////////////////////
@@ -367,6 +381,15 @@ on a new line! ""Wow!"", the masses cried";
}
// Methods can have the same name, as long as the signature is unique
+ // A method that differs only in return type is not unique
+ public static void MethodSignatures(
+ ref int maxCount, // Pass by reference
+ out int count)
+ {
+ count = 15; // out param must be assigned before control leaves the method
+ }
+
+ // Methods can have the same name, as long as the signature is unique
public static void MethodSignatures(string maxCount)
{
}
@@ -400,6 +423,10 @@ on a new line! ""Wow!"", the masses cried";
MethodSignatures(3, 1, 3, "Some", "Extra", "Strings");
MethodSignatures(3, another: 3); // explicity set a parameter, skipping optional ones
+ // BY REF AND OUT PARAMETERS
+ int maxCount = 0, count; // ref params must have value
+ MethodSignatures(ref maxCount, out count);
+
// EXTENSION METHODS
int i = 3;
i.Print(); // Defined below
@@ -682,7 +709,7 @@ on a new line! ""Wow!"", the masses cried";
// All though this is not entirely useful in this example, you
// could do bicycle[0] which yields "chris" to get the first passenger or
// bicycle[1] = "lisa" to set the passenger. (of this apparent quattrocycle)
- private string[] passengers = { "chris", "phil", "darren", "regina" }
+ private string[] passengers = { "chris", "phil", "darren", "regina" };
public string this[int i]
{
@@ -784,7 +811,7 @@ on a new line! ""Wow!"", the masses cried";
/// EntityFramework Code First is awesome (similar to Ruby's ActiveRecord, but bidirectional)
/// http://msdn.microsoft.com/en-us/data/jj193542.aspx
/// </summary>
- public class BikeRepository : DbSet
+ public class BikeRepository : DbContext
{
public BikeRepository()
: base()