Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

C Basics Cheat Sheet

Www.Begincodingnow.Com
Visit to download the full and correct content document:
https://textbookfull.com/product/c-basics-cheat-sheet-www-begincodingnow-com/
More products digital (pdf, epub, mobi) instant
download maybe you interests ...

Commander in Cheat How Golf Explains Trump Rick Reilly

https://textbookfull.com/product/commander-in-cheat-how-golf-
explains-trump-rick-reilly/

Japan Style Sheet Swet (Society Of Writers

https://textbookfull.com/product/japan-style-sheet-swet-society-
of-writers/

C Programming Complete Guide to Learn the Basics of C


Programming in 7 days 2nd Edition Xavier S Martin

https://textbookfull.com/product/c-programming-complete-guide-to-
learn-the-basics-of-c-programming-in-7-days-2nd-edition-xavier-s-
martin/

Tribology in Sheet Rolling Technology 1st Edition Akira


Azushima (Auth.)

https://textbookfull.com/product/tribology-in-sheet-rolling-
technology-1st-edition-akira-azushima-auth/
Sheet Metal Forming Optimization: Bioinspired
Approaches 1st Edition Ganesh M. Kakandikar

https://textbookfull.com/product/sheet-metal-forming-
optimization-bioinspired-approaches-1st-edition-ganesh-m-
kakandikar/

Sheet metal meso- and microforming and their industrial


applications First Edition Fu

https://textbookfull.com/product/sheet-metal-meso-and-
microforming-and-their-industrial-applications-first-edition-fu/

Manufacturing Integrated Design Sheet Metal Product and


Process Innovation 1st Edition Peter Groche

https://textbookfull.com/product/manufacturing-integrated-design-
sheet-metal-product-and-process-innovation-1st-edition-peter-
groche/

China s National Balance Sheet Theories Methods and


Risk Assessment 1st Edition Yang Li

https://textbookfull.com/product/china-s-national-balance-sheet-
theories-methods-and-risk-assessment-1st-edition-yang-li/

Ethics the Basics Mizzoni

https://textbookfull.com/product/ethics-the-basics-mizzoni/
Type Default Value Reference/Value
C# Basics Cheat Sheet (1 of 4) .NET tools (VS, compiler, debugger, ASP and WCF) to produce compiled
code that uses the Base Class Library (BCL) that are all used by the CLR. All numbers 0 Value Type
begincodingnow.com
The compiler for a .NET language takes a source code (C# code and Boolean False Value Type
others) file and produces an output file called an assembly (EXE or DLL), String null Reference Type
Introduction to C# which isn’t native machine code but contains an intermediate language Char ‘\0’ Value Type
The C# language was developed by Microsoft for the .NET framework. called the Common Intermediate Language (CIL), and metadata. The Struct Value Type
C# is a completely-rewritten language based on C Language and C++ program’s CIL isn’t compiled to native machine code until it’s called to Enum E(0) Value Type
Language. It is a general-purpose, object-oriented, type-safe platform- run. At run time, the CLR checks security, allocates space in memory Nullable null Value Type
neutral language that works with the .NET Framework. Class null Reference Type
and sends the assembly’s executable code to its just-in-time (JIT)
Interface Reference Type
compiler, which compiles portions of it to native (machine) code. Once
Array Reference Type
Visual Studio (VS) the CIL is compiled to native code, the CLR manages it as it runs, Delegate Reference Type
Visual Studio Community 2017 is a free download from Microsoft. To performing such tasks as releasing orphaned memory, checking array
create a new project, go to File ➤ New ➤ Project in Visual Studio. bounds, checking parameter types, and managing exceptions.
From there select the Visual C# template type in the left frame. Then Compilation to native code occurs at run time. In summary, the steps Reference Types & Value Types
C# types can be divided into value types and reference types. Value
select the Console App template in the right frame. At the bottom of are: C# code ➤assembly (exe or dll) & BCL ➤ CLR & JIT compiler
types comprise most built-in types (specifically, all numeric types, the
the window configure the name and location of the project. Click OK ➤machine code ➤ operating system ➤ machine.
char type, and the bool type) as well as custom struct and enum types.
and the project wizard will create your project.
There are two types of value types: structs and enumerations.
Variable Declaration and Assignment Reference types comprise all class, array, delegate, and interface types.
C# Hello World (at the Console) In C#, a variable must be declared (created) before it can be used. To Value types and reference types are handled differently in memory.
using System;
namespace ConsoleApp1
declare a variable, you start with the data type you want it to hold Value types are stored on the stack. Reference types have a reference
{ followed by a variable name. A value is assigned to the variable by using (memory pointer) stored on the stack and the object itself is stored on
class Program the equals sign, which is the assignment operator (=). The variable then the heap. With reference types, multiple variables can reference the
{
static void Main(string[] args) becomes defined or initialized. same object, and object changes made through one variable will affect
{ other variables that reference the same object. With value types, each
Console.WriteLine("Hello World");
/* this comment in C# is ignored by compiler */ Data Types variable will store its own value and operations on one will not affect
/* a multi-line comment A primitive is a C# built-in type. A string is not a primitive type but it is a another.
that is ignored by the compiler*/
}
built-in type.
} Primitive Bytes Suffix Range Sys Type Strings
} bool 1 True or False Boolean A string is a built-in non-primitive reference type that is an immutable
char 2 Unicode Char sequence of Unicode characters. A string literal is specified between
Ctrl+F5 will run the program without the debug mode. The reason why
byte 1 0 to 255 Byte double quotes. The + operator concatenates two strings. A string
you do not want to choose the Start Debugging command (F5) here is sbyte 1 -128 to 127 SByte
because the console window will then close as soon as the program has preceded with the $ character is called an interpolated string which can
short 2 -32,768 to Int16
finished executing, unless you use Console.ReadKey(); at the end. 32,767 include expressions inside braces { } that can be formatted by
There are several methods and properties of console. You can change int 4 -231 to 231-1 Int32 appending a colon and a format string.
string s = $"255 in hex is {byte.MaxValue:X2}";
colors and put a Title on the console. Add this to the Main() to use your long 8 L –263 to 263–1 Int64
ushort 2 0 to 216-1 UInt16 Interpolated strings must complete on a single line, unless you also
namespace, which may be your solution and project name also.
Type myType = typeof(Program); uint 4 U 0 to 232-1 UInt32 specify the verbatim string operator. Note that the $ operator must
Console.Title = myType.Namespace; ulong 8 UL 0 to 264-1 UInt64 come before @ as shown here:
Console.ForegroundColor = ConsoleColor.Red; int x = 2;
Console.WindowWidth = 180; // max might be 213 (180 is very wide) float 4 F +-1.5 x 10-45 to Single string s = $@"this spans {
+-3.4 x 1038 x} lines in code but 1 on the console.";
double 8 D +-5.0 x 10 to
-324
Double Another example:
+-1.7 x 10308
A Few Code Snippets in VS decimal 16 M +-1.0 x 10 to
-28
Decimal
string s = $@"this spans {x}
lines in code and 2 lines on the console."; // at left side of editor
Code Snippet Description +-7.9 x 1028 string does not support < and > operators for comparisons. You must
cw Console.WriteLine() The numeric suffixes listed in the preceding table explicitly define the instead use string’s CompareTo method, which returns a positive
prop public int MyProperty { get; set; }
type of a literal. By default, the compiler infers a numeric literal to be number, a negative number, or zero.
ctor Constructor either of type double or an integral type:
Ctrl+K+C/Ctrl+K+U Comment & un-comment a selected code block • If the literal contains a decimal point or the exponential symbol (E), it
F12 Go to Definition Char
is a double. C#’s char type (aliasing the System.Char type) represents a Unicode
ReSharper is a plug-in for Visual Studio that adds many code navigation • Otherwise, the literal’s type is the first type in this list that can fit the
and editing features. It finds compiler errors, runtime errors, character and occupies two bytes. A char literal is specified inside single
literal’s value: int, uint, long, and ulong. quotes.
redundancies, and code smells right as you type, suggesting intelligent • Integral Signed (sbyte, short, int, long) char MyChar = 'A';
corrections for them. char[] MyChars = { 'A', 'B', 'C' };
• Integral Unsigned (byte, ushort, uint, ulong)
Console.WriteLine(MyChar);
• Real (float, double, decimal) foreach (char ch in MyChars) { Console.Write(ch); }
Common Language Runtime (CLR) Console.WriteLine(2.6.GetType()); // System.Double
A core component of the .NET Framework is the CLR, which sits on top Console.WriteLine(3.GetType()); // System.Int32

of the operating system and manages program execution. You use the
C# Basics Cheat Sheet (2 of 4) left and right side are true, and logical or (||) evaluates to true if either
the left or right side is true. The logical not (!) operator is used for
can chain these methods together because each of these methods
return a StringBuilder object.
begincodingnow.com
inverting a Boolean result. The bitwise operators can manipulate static void Main(string[] args)
{
individual bits inside an integer. A few examples of Operators. var sbuild = new System.Text.StringBuilder("");
Escape Sequences Symbol Name Example Overloadable? sbuild.AppendLine("Title")
Escape sequences work with chars and strings, except for verbatim . Member access x.y No .Append('=', 5)
.Replace('=', '-')
strings, which are proceeded by the @ symbol. () Function call x() No .Insert(0, new string('-', 5))
Console.WriteLine("Hello\nWorld"); // on two lines
Console.WriteLine("Hello\u000AWorld"); // on two lines
[] Array/index a[x] Via indexer .Remove(0, 4);
++ Post-increment x++ Yes Console.WriteLine(sbuild);
char newLine = '\n'; }
Console.WriteLine("Hi" + newLine + "World"); // on two lines -- Post-decrement x-- Yes
The \u (or \x) escape sequence lets you specify any Unicode character new Create instance new Foo() No
via its four-digit hexadecimal code. ?. Null-conditional x?.y No Arrays
Char Meaning Value ! Not !x Yes An array is a fixed number of elements of the same type. An array uses
\’ Single quote 0x0027 ++ Pre-increment ++x Yes square brackets after the element type. Square brackets also index the
\” Double quote 0x0022 -- Pre-decrement --x Yes array, starting at zero, not 1.
\\ Backslash 0x005C () Cast (int)x No static void Main(string[] args)
{
\0 Null 0x0000 == Equals x == y Yes int[] numArray = { 7, 2, 3 };
\a Alert 0x0007 != Not equals x != y Yes int[] numArray2 = new int[3]; // default value is 0
\b Backspace 0x0008 & Logical And x&y Yes // below is 3 rows and 2 columns
| Logical Or x|y Yes int[,] numArray3 = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
\f Form feed 0x000C char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };
\n New line 0x000A && Conditional And x && y Via & char[] vowels2 = { 'a', 'e', 'i', 'o', 'u' }; // simplified
\r Carriage return 0x000D || Conditional Or x || y Via| Array.Sort(numArray);
\t Horizontal tab 0x0009 ? : Ternary isTrue ? then No foreach (int n in numArray) { Console.Write(n); } // 237
Console.WriteLine("First element is: " + numArray[0]); // 2
\v Vertical tab 0x000B this : elseThis }
= Assign x = 23 No
Verbatim string literals. A verbatim string literal is prefixed with @ and An array itself is always a reference type object, regardless of element
*= Multiply by self x *= 3 Via *
does not support escape sequences. (and / + -) type. For integer types the default is zero and for reference types the
string myPath = @"C:\temp\"; default is null. For Boolean the default is False.
string myPath = "C:\\temp\\"; => Lambda x => x + 3 No
int[] a = null; // this is legal since arrays themselves are ref tyes

Note: The && and || operators are conditional versions of the & and |
Constants Rectangular & Jagged Arrays
operators. The operation x && y corresponds to the operation x & y,
A local constant is much like a local variable, except that once it is With rectangular arrays we use one set of square brackets with the
except that y is evaluated only if x is not false. The right-hand operand
initialized, its value can’t be changed. The keyword const is not a number of elements separated by a comma. Jagged arrays are arrays of
is evaluated conditionally depending on the value of the left-hand
modifier but part of the core declaration and it must be placed arrays, and they can have irregular dimensions. We use 2 sets of square
operand. x && y is equivalent to x ? y : false
immediately before the type. A constant is a static field whose value brackets for jagged arrays.
The ?? operator is the null coalescing operator. If the operand is non-
can never change. A constant is evaluated statically at compile time and
null, give it to me; otherwise, give me a default value.
the compiler literally substitutes its value whenever used (rather like a static void Main(string[] args)
macro in C++). A constant can be any of the built-in numeric types, {
bool, char, string, or an enum type. The using Directive // a jagged array with 3 rows
string[][] a = new string[3][];
const int myNumber = 3; To access a class from another namespace, you need to specify its fully a[0] = new string[1]; a[0][0] = "00";
qualified name, however the fully qualified name can be shortened by a[1] = new string[3]; a[1][0] = "10"; a[1][1] = "11";
a[1][2] = "12";
Expressions including the namespace with a using directive. It is mandatory to a[2] = new string[2]; a[2][0] = "20"; a[2][1] = "21";
An expression essentially denotes a value. The simplest kinds of place using directives before all other members in the code file. In foreach (string[] b in a)
expressions are constants (such as 45) and variables (such as myInt). Visual Studio, the editor will grey out any using statements that are not {
foreach (string c in b)
Expressions can be transformed and combined with operators. An required. {
operator takes one or more input operands to output a new Console.Write(c + " ");
}
expression. StringBuilder }
System.Text.StringBuilder Console.WriteLine("initialize them");
string[][] e = { new string[] { "00" },
Operators There are three Constructors new string[] { "10", "11", "12" },
Operators are used to operate on values and can be classed as unary, StringBuilder sb = new StringBuilder(); new string[] { "20", "21" } };
binary, or ternary, depending on the number of operands they work on StringBuilder sb = new StringBuilder(myString);
foreach (string[] f in e)
(one, two, or three). They can be grouped into five types: arithmetic, StringBuilder sb = new StringBuilder(myString,capacity); {
assignment, comparison, logical and bitwise operators. The arithmetic Capacity is initial size (in characters) of buffer. foreach (string g in f)
{
operators include the four basic arithmetic operations, as well as the The string class is immutable, meaning once you create a string object Console.Write(g + " ");
modulus operator (%) which is used to obtain the division remainder. you cannot change its content. If you have a lot of string manipulations }
to do, and you need to modify it, use StringBuilder. Note that you }
The second group is the assignment operators. Most importantly, the }
assignment operator (=) itself, which assigns a value to a variable. The cannot search your string. You do not have the following: IndexOf(),
comparison operators compare two values and return either true or StartsWith(), LastIndexOf(), Contains() and so on. Instead you have
false. The logical operators are often used together with the methods for manipulating strings such as Append(), Insert(), Remove(),
comparison operators. Logical and (&&) evaluates to true if both the Clear() and Replace(). StringBuilder needs using System.Text. You
C# Basics Cheat Sheet (3 of 4) Formatting Numerics Struct
begincodingnow.com Numbers fall into two categories: integral and floating point. You can define a custom value type with the struct keyword. A struct
Format Pattern Value Description is a value type. Fields cannot have an initializer and cannot inherit from
Specifier a class or be inherited. The explicit constructor must have a parameter.
DateTime C or c {0:C2}, 2781.29 $2781.29 Currency struct Customer
DateTime is a struct and is therefore a value type. D or d {0:D5}, 78 00078 Must be integer {
var dateTime = new DateTime(2000, 1, 1); public string firstName;
var now = DateTime.Now; // gets the current date & time
E or e {0:E2}, 2781.29 2.78+E003 Must be floating public string lastName;
var today = DateTime.Today; // gets the current date (no time) point public string middleName;
var utcnow = DateTime.UtcNow; F or f {0:F2}, 2781.29 2781.29 Fixed point public int birthYear;
Console.WriteLine($"The current hour is: {now.Hour}"); G or g {0:G5}, 2781.29 2781.2 General //public string Name() => firstName + " " + middleName + " " + las
Console.WriteLine($"The current minute is: {now.Minute}"); tName;
N or n {0:N1}, 2781.29 2,781.29 Inserts commas public string Name() => firstName + " " +
Console.WriteLine($"The current second is: {now.Second}");
var tomorrow = now.AddDays(1); P or p {0:P3}, 4516.9 45.16% Converts to percent (String.IsNullOrWhiteSpace(middleName) ? "" :
var yesterday = now.AddDays(-1); R or r {0:R}, 2.89351 2.89315 Retains all decimal middleName + " ") + lastName;
// AddDays, AddHours, AddMinutes, AddMonths, AddYears etc. places (round-trip) // Name() accesses firstName & lastName; uses lambda and ternary
Console.WriteLine($"Tomorrow (yyyy-mm-dd): {tomorrow}"); X or x {0,9:X4}, 17 0011 Converts to Hex public string NameFunct()
Console.WriteLine(now.ToLongDateString()); {
Console.WriteLine(now.ToShortDateString()); string midN = String.IsNullOrWhiteSpace(middleName) ?
Console.WriteLine(now.ToLongTimeString()); Console.WriteLine("Value: {0:C}.", 447); // $447.00 "" : middleName + " ";
Console.WriteLine(now.ToShortTimeString()); int myInt = 447; return firstName + " " + midN + lastName;
Console.WriteLine(now.ToString()); // shows date and time Console.WriteLine($"Value: {myInt:C}"); // $ is interpolation $447.00 }
Console.WriteLine(now.ToString("yyyy-MM-dd")); // format specifier The optional alignment specifier represents the minimum width of the }
Console.WriteLine(now.ToString("yyyy-MMMM-dd")); // format specifier class Program
Console.WriteLine(now.ToString("dddd yyyy-MMMM- dd"));
field in terms of characters. It is separated from the index with a {
Console.WriteLine(now.ToString("yyyy-MM-dd HH:mm:ss")); comma. It consists of a positive or negative integer. The sign represents static void Main(string[] args)
Console.WriteLine(String.Format("today: {0:D}", now)); either right (positive) or left (negative) alignment. {
Console.WriteLine(String.Format("today: {0:F}", now)); Console.WriteLine("Value: {0, 10:C}", myInt); // + right align Customer myCustomer;
// D F d f g G M m Y y t T s u U Console.WriteLine("Value: {0, -10:C}", myInt); // - left align myCustomer.firstName = "Sam";
myCustomer.lastName = "Smith";
Value: $447.00 myCustomer.middleName = " "; // note the spaces
TimeSpan Value: $447.00 myCustomer.birthYear = 1960;
// Creating TimeSpan object - there are 3 ways. Console.WriteLine($"Value: {myInt, 10:C}"); // interpolation Console.WriteLine($"{myCustomer.Name()} was born in {myCustome
var timeSpan = new TimeSpan(2, 1, 45); // hours minutes second Value: $447.00 r.birthYear}.");
// Creating TimeSpan object - there are 3 ways. Console.WriteLine("Percent: {0:P2}",0.126293); // 12.63 rounds Console.WriteLine($"{myCustomer.NameFunct()} was born in {myCu
var timeSpan = new TimeSpan(2, 1, 45); // hours minutes seconds Console.WriteLine("{0:E2}", 12.6375);//2 decimal places 1.26E+001 stomer.birthYear}.");
var timeSpan1 = new TimeSpan(3, 0, 0); // 3 hours // Output: Sam Smith was born in 1960.
// second way: // Output: Sam Smith was born in 1960.
// easier to know it is one hour with FromHours() Enumerated Type }
}
var timeSpan2 = TimeSpan.FromHours(1);
// third way: It can be defined using the enum keyword directly inside a namespace,
var now = DateTime.Now; class, or structure.
var end = DateTime.Now.AddMinutes(2); public enum Score Conversions
var duration = end - now; { C# can convert between instances of compatible types. A conversion
Console.WriteLine("Duration: " + duration); Touchdown = 6, FieldGoal = 3, Conversion = 1, Safety = 2,
// above result is: Duration: 00:02:00.00199797 }
always creates a new value from an existing one. Conversions can be
var negativeduration = now - end; class Program either implicit or explicit: implicit conversions happen automatically,
Console.WriteLine("\"Negative Duration\": " + duration); // positive { whereas explicit conversions require a cast. One useful use of a
number static void Main(string[] args)
{ conversion is when you are getting input from the user in a Console
TimeSpan trueEnd = now.AddMinutes(2) - now; // subtract to get TimeSpa Console.WriteLine(Score.Touchdown);// output: Touchdown program, using Convert().
n object int myInt = (int)Score.FieldGoal; Console.WriteLine("Enter your number: ");
Console.WriteLine("True Duration: " + trueEnd); Console.WriteLine(myInt); // output: 3 double number = Convert.ToDouble(Console.ReadLine());
// above output: True Duration: 00:02:00 Score myScore = (Score)6;
Console.WriteLine(myScore); // output: Touchdown
Here are some examples below using implicit and explicit conversions.
// Properties string teamscore = "Conversion"; int x = 12345; // int is a 32-bit integer
// timeSpan is two hours, one minutes and 45 seconds Enum.TryParse(teamscore, out Score myVar); long y = x; // implicit conversion to 64-bit long
Console.WriteLine("Minutes: " + timeSpan.Minutes); Console.WriteLine(myVar); // output: Conversion short z = (short)x; // cast - explicit conversion to 16-bit int
Console.WriteLine("Total Minutes: " + timeSpan.TotalMinutes); Console.WriteLine((int)myVar); // output: 1 Console.WriteLine(z);
Console.WriteLine("Total Days: " + timeSpan.TotalDays); } byte b = (byte)x; // data loss !!
} Console.WriteLine(b); // 57
// Add Method of TimeSpan // 12345 = 0011 0000 0011 1001
Enumerations could just be a list of words. For example you could have // 57 = 0011 1001
// Add 3 min to our original TimeSpan 2 hours 1 minutes 45 seconds
Console.WriteLine("Add 3 min: " + timeSpan.Add(TimeSpan.FromMinutes(3) a list of the days of the week: Monday, Tuesday and so on, without any int myInt = 1_000_000; // C# 7 allows underscores
Console.WriteLine(2.6.GetType()); // System.Double
)); values. You can use IsDefined() and typeof(). Console.WriteLine(3.GetType()); // System.Int32
Console.WriteLine("Add 4 min: " + timeSpan.Add(new TimeSpan(0,4,0)));
// ToString method
Console.WriteLine("ToString: " + timeSpan.ToString());
if(Enum.IsDefined(typeof(Score), “Safety”)… Conversions from integral types to real types are implicit, whereas the
// don't need ToString here: reverse must be explicit. Converting from a floating-point to an integral
Console.WriteLine("ToString not needed: " + timeSpan);
// Parse method
The Object Class type truncates any fractional portion; to perform rounding conversions,
Console.WriteLine("Parse: " + TimeSpan.Parse("01:02:03")); In .NET, everything is an object and the base of everything is the use the static System.Convert class.
Object class. The available methods of an object are: Equals, float f = 128.67F;
int d = Convert.ToInt32(f); // rounds
GetHashCode, GetType and ToString. // System.Int32 d is 129
Console.WriteLine(d.GetType() + " d is " + d);
C# Basics Cheat Sheet (4 of 4) string[] files = Directory.GetFiles(@"D:\temp\folder1", "*.*",
SearchOption.AllDirectories); // or TopDirectoryOnly
begincodingnow.com Lists
foreach (var file in files){Console.WriteLine(file);}
Lists are covered in more detail in the Lists of Objects section in the
var directories = Directory.GetDirectories(@"D:\temp","*.*",
Advanced section, but are here now due to their importance.
The Regex Class var numbers = new List<int>() {1,2,3,4};
SearchOption.AllDirectories);
foreach (var dir in directories)
The class is System.Text.RegularExpressions.Regex numbers.Add(1); {
Pattern Desscription Example numbers.AddRange(new int [3] {5,6,7}); Console.WriteLine(dir);
foreach (var num in numbers) Console.Write(num + " "); }
+ Matches one or more ab+c matches abc, abbc var directoryInfo = new DirectoryInfo(@"D:\temp\folder1");
* Matches zero or more ab*c matches ac, abbc var ct = directoryInfo.CreationTime;
? Matches zero or one ab?c matches ac, abc File IO Console.WriteLine("Creation date and time: " + ct);
}
\d Any digit from 0 to 9 \d\d matches 14, 98, 03 using System; }
[0-9] Any digit from 0 to 9 [0-9] matches 3, 8, 1, 0, 2 using System.IO; // add this }
\d{3} Any 3 digits from 0-9 \d{3} matches 123, 420 namespace FileManipulation
{
[0-9]{3} Any 3 digits from 0-9 [0-9]{3} matches 123, 420 class Program Debugging
{// File (statis) and FileInfo (instance) To debug your code you first decide where in your code you suspect a
Comparison Operators static void Main(string[] args)
{ problem and create a breakpoint by putting the cursor on that line and
The comparison operators compare two values and return true or false. var filenamewithpath = @"D:\myfile.txt"; // verbatim @ pressing F9. Press F5 to run the program in debug mode. You can use
They specify conditions that evaluate to true or false (like a predicate): using (File.Create(filenamewithpath))
// without using you get Unhandled Exception
multiple breakpoints if you want. We can either use F10 to step over or
== != > < >= <= // true will over-write existing file perhaps F11 to step into. Place your cursor over a variable and you
File.Copy(filenamewithpath, @"D:\myfile_2.txt", true); should be able to see the data inside. If all looks good, go ahead and
File.Copy(filenamewithpath, @"D:\myfile_3.txt", true);
Conditional Statements File.Delete(@"D:\myfile_3.txt"); press F10 or perhaps F11. If you have another breakpoint, you can
Syntax Example if(File.Exists(@"D:\myfile_2.txt")) press F5 to run to the next breakpoint. Also, you can move the current
{
if (condition) { if (product == "H1")
Console.WriteLine("File " + @"D:\myfile_2.txt" + " exists.");
position of execution backwards by dragging the yellow arrow at the
// statements price = 134.00M;// M decimal left. When you are done you can press Shift+F11 to step out. You can
}
else if (product == "H2")
} else {
price = 516.00M;
string filecontent = File.ReadAllText(filenamewithpath); end the debugging with Shift+F5. You can run it without the debugger
// statements var fileInfo = new FileInfo(filenamewithpath);
else price = 100.00M;
fileInfo.CopyTo(@"D:\myfile_4.txt", true); with Ctrl+F5. You can manage all your breakpoints with the Breakpoints
window. Debug ➤ Windows ➤ Breakpoints.
}
var fileInfo4 = new FileInfo(@"D:\myfile_4.txt");
q ? a : b, price = (product == "A1") ? if (fileInfo4.Exists) // Exists is a property
if condition q is true, a is 34 : 42; {
evaluated, else b is evaluated. // ternary operator ? : fileInfo4.Delete(); // takes no paramters It’s a good idea to always check that the methods you write receive
switch (product) } meaningful data. For example, if you expect a list of something, check
switch (expression)
{ else
{ case expression: { that the list is not null. Users may not enter values you expect. It’s
case "P1": price = 15; break;
// statements
case "P2": price = 16; break; Console.WriteLine("Cannot delete file " important to think of these Edge Cases, which are uncommon
break / goto / return() case ... + @"D:\myfile_4.txt" + " because it does not exist.");
default: price = 10M; break;
}
scenarios, which is the opposite of the Happy Path.
default: }
// statements // FileInfo does not have a ReadAllText method
// need to call openread which returns a file string but
break / goto / return()
// that is a little bit complex. NuGet Package Manager
} Console.WriteLine("Press any key to continue..."); NuGet is the package manager for .NET. The NuGet client tools provide
// expression may be integer, }
string, or enum }
the ability to produce and consume packages. The NuGet Gallery is the
} central package repository used by all package authors and consumers..
Loops Use File for occasional usage and FileInfo for many operations because Packages are installed into a Visual Studio project using the Package
Syntax Example each time you use File the OS does security checks and that can slow Manager UI or the Package Manager Console. One interesting package
var i = 1;
var total = 0;
down your app; with FileInfo you need to create an instance of it. Both is the HtmlAgilityPack that allows you to parse HTML, but there are lots
while (condition)
{ body } while (i <= 4) are easy to use. StreamReader and StreamWriter are available. You can of them.
{ // 1 + 2 + 3 + 4 = 10 encode in ASCII, Unicode, BigEndianUnicode, UTF-8, UTF-7, UTF-32 and
total = total + i;
i++; Default. Different computers can use different encodings as the default, Your Own Library (Assembly)
} but UTF-8 is supported on all the operating systems (Windows, Linux, To create a class library using Visual Studio 2017 Community, in the
do
do { body } { // 1 + 2 + 3 + 4 + 5 = 15
and Max OS X) on which .NET Core applications run.. menu select File ➤ New ➤ Project ➤ Installed ➤ Visual C# ➤ .NET
var filenamewithpath = @"D:\temp\A_ascii.txt";
Standard ➤ Class Library(.NET Standard) and give it a name and
while (condition); total = total + i;
File.WriteAllText(filenamewithpath, "A", Encoding.ASCII);
i++;
} location and press OK. Write your library code. Switch to Release from
while (i <= 4); Directory IO Build. Press Ctrl+Shift+B to build the DLL. Note the location of the DLL
for (var i = 1; i < list.Count; i++)
{
(bin\Release\netstandard2.0). Within the project that uses the library,
for (initializer; using System;
termination condition; if (list[i] < min) using System.IO; // add this you need to give the compiler a reference to your assembly by giving its
iteration;)
}
min = list[i]; namespace Directories name and location. Select Solution Explorer ➤ Right-click the
References folder ➤ Add Reference. Select the Browse tab, browse to
{ // statements } {
class Program
foreach (type identifier in int[] nums = new int[]{ 2, 5, 4};
{ the DLL file mentioned above. Click the OK button. For convenience you
static void Main(string[] args)
collection) foreach (int num in nums) {
can now add a using statement at the top of your program. You
{ // statements } { Directory.CreateDirectory(@"D:\temp\folder1"); should now have access to your library code. Nice!
Console.WriteLine(num); File.Create(@"D:\temp\folder1\mytext.txt");
} File.Create(@"D:\temp\folder1\mytext2.txt");
C# OOP Cheat Sheet (1 of 4) value for each type is 0 and is false for bool. The default for reference
types is null.
begincodingnow.com
Abstract Classes class Order
{
Object-Oriented Programming (OOP) A class declared as abstract can never be instantiated. Instead, only its
}
public int Id;

Object-oriented programming (OOP) is a programming paradigm that concrete subclasses can be instantiated. Abstract classes can define class Customer
employs objects to encapsulate code. Objects consist of types and are abstract members which are like virtual members, except they don’t {
called classes. A class is just a template for an object which is an provide a default implementation. That implementation must be public int Id;
public string Name;
instance of the class, which occupies memory. When we say that a class provided by the subclass, unless that subclass is also declared abstract. public readonly List<Order> Orders = new List<Order>();
is instantiated, we mean that an object in memory has been created. // Note: no parameterless constructor for Customer
public Customer(int id)
Classes contain data and executable code. Everything in C# and .NET is Sealed Classes { // a constructor
an object. In the menu View ➤ Object Browser. A sealed class cannot be used as the base class for any other class. You this.Id = id; // the keyword this is redundant
}
use the sealed keyword to protect your class from the prying methods public Customer(int id, string name) : this(id)
Programming Principles of a subclass. Static classes are implicitly sealed. { // a constructor
this.Name = name; // the keyword this is redundant
DRY is an acronym for Don’t Repeat Yourself. In OOP, encapsulation is }
used to refer to one of two related but distinct notions, and sometimes Instance Constructors public void DoSomething() { } // just an example method
to the combination thereof: (1) A language mechanism for restricting For classes, the C# compiler automatically generates a parameterless
}
direct access to some of the object's components. (2) A language public constructor if and only if you do not define any constructors. class Program
construct that facilitates the bundling of data with the methods (or However, as soon as you define at least one constructor, the {
static void Main(string[] args)
other functions) operating on that data. In OOP, the open/closed parameterless constructor is no longer automatically generated, so you {
principle states that software entities (classes, modules, functions, etc.) may need to write it yourself. var customer = new Customer(3, "Bob");
customer.Orders.Add(new Order());
should be open for extension, but closed for modification. customer.Orders.Add(new Order() { Id = 7 });
Instance constructors execute when the object is first instantiated. Console.WriteLine("Customer Id: " + customer.Id + " Name: "
Simple Class Declaration When an object is destroyed the destructor is called. Memory is freed + customer.Name);
Console.WriteLine("Num orders: " + customer.Orders.Count);
The simplest class declaration is: up at this time. Constructors are called with the new keyword. foreach (var ord in customer.Orders) { Console.WriteLine("O
class Foo { } rder Id: " + ord.Id); }
Constructors can be static. A static constructor executes once per type, }
[ public | protected | internal | private ] }
rather than once per instance. A type can define only one static
[ abstract | sealed | static ] Here is the Console output of he above program. Notice that the first
constructor, and it must be parameterless and have the same name as
class class_name [:class/interfaces inherited from ] order Id below is zero because zero is the default.
the type. Customer Id: 3 Name: Bob
public class Customer
A class is a data structure that can store data and execute code. It { Number of orders: 2
contains data members and function members. The members can be public string Name; // in real world these are private Order Id: 0
public int Id; // in real world these are private
any combination of nine possible member types. A local variable is a public Customer() { } // constructor (same name as class)
Order Id: 7
variable declared inside a function member. On the Internet are the public Customer(int id) // constructor Generally, you would use private fields with public properties to
StyleCop Rules Documentation the ordering of members in classes.
{ provide encapsulation.
this.Id = id; // set Id property
Note: Whenever you have a class, such as our Customer, and inside }
that class you have a List of objects of any type, you should always public Customer(int id, string name) // constructor
{
Methods
initialize that list to an empty list. this.Id = id; // 'this' references current object Customer A method is a named block of code that is a function member of a class.
this.Name = name; // here we set Name property You can execute the code from somewhere else in the program by
}
Static }
using the method’s name, provided you have access to it. Below is the
Static classes are meant to be consumed without instantiating them. class Program simplest way to write a method inside a class.
{ class NotAnything { void DoNothingMethod() { } }
Static classes can be used to group members that are to be available static void Main(string[] args) You can also pass data into a method and receive data back as output.
throughout the program. A static class must have all members marked {
A block is a sequence of statements between curly braces. It may
// ERROR: not contain constructor that takes zero arguments
as static as well as the class itself. The class can have a static // unless we create OUR OWN parameterless constructor (we did) contain local variables (usually for local computations), flow-of-control
constructor, but it cannot have an instance constructor. Static classes var customer = new Customer();
statements, method invocations, nested blocks or other methods
are implicitly sealed, meaning you cannot inherit from a static class. A customer.Id = 7;
customer.Name = "John"; known as local functions.
non-static instantiable class can have static members which exist and Console.WriteLine(customer.Id);
are accessible even if there are no instances of the class. A static field is Console.WriteLine(customer.Name);
[access modifier]
}
shared by all the instances of the class, and all the instances access the } [static|virtual|override|new|sealed|abstract]
same memory location when they access the static field. Static method name (parameter list) { body }
methods exist. Static function members cannot access instance
Fields
members but can access other static members. Static members, like C# allows for optional parameters which you can either include or omit
A field is a variable that belongs to a class. It can be of any type, either
instance members, can also be accessed from outside the class using when invoking the method. To specify that, you must include a default
predefined or user-defined. A field initializer is part of the field
dot-syntax notation. Another option to access the member doesn’t use value for that parameter in the method declaration. Value types
declaration and consists of an equal sign followed by an expression that
any prefix at all, if you have included a using static declaration for the require the default value to be determinable at compile time, and
evaluates to a value. The initialization value must be determinable at
specific class to which that member belongs: reference types only if the default value is null. The declaration order
compile time. If no initializer is used, the compiler sets the value of a
Using static System.Console; must be all required (if any) – all optional (if any) – all params (if any).
field to a default value, determined by the type of the field. The default
C# OOP Cheat Sheet (2 of 4) The get and set denote property accessors. The set method could
{
get { return words[wordNum]; }
begincodingnow.com set { words[wordNum] = value; }
throw an exception if value was outside a valid range of values. }
}
Access Description static void Main(string[] args)
Modifier Object Initialization Syntax {
string s = "hello world";
public Fully accessible. This is the implicit accessibility for C# 3.0 (.NET 3.5) introduced Object Initializer Syntax, a new way to Console.WriteLine(s[0]); // 'h' zero-based
members of an enum or interface. initialize an object of a class or collection. Object initializers allow you Console.WriteLine(s[5]); // ' '
private Accessible only within the containing type. This is the to assign values to the fields or properties at the time of creating an string str = null;
Console.WriteLine(str?[0]); // Writes nothing; no error.
default accessibility for members of a class or struct. object without invoking a constructor. // Console.WriteLine(str[0]); // NullReferenceException
Perhaps you have a method that is implementation class Program
{ Sentence sen = new Sentence();
detail that calculates something. public class Person Console.WriteLine(sen[1]); // quick
protected Accessible only within the containing type or { sen[3] = "wildebeest"; // replace the 4th word
subclasses (derived classes). May be a sign of bad public int id { get; set; } Console.WriteLine(sen[3]); // wildebeest
design. public string FirstName { get; set; } for (int i=0;i<sen.Length;i++) { Console.Write(sen[i] + "|"); }
public string LastName { get; set; }
internal Accessible only from the same assembly. We create a public DateTime BirthDate { get; set; }
// now use our constructor to use our sentence
Sentence sent = new Sentence("The sleeping black cat");
separate class library and use internal. How? Right- } Console.WriteLine(sent[1]); // sleeping
click Solution ➤ Add ➤ New Project ➤ Class Library static void Main(string[] args)
{ // don't need to initialize all fields
}

(DLL). We’ll need to add a Reference (Project, Add var p = new Person {FirstName = "J", LastName = "Smith"};
You have your own class Customer with fields FirstName and
Reference) and add using statement. Console.WriteLine("Last name is {0}", p.LastName); LastName. Instantiate it as Cust1. Get the first name and last name
protected Not used normally! Accessible only from the same
// OUTPUT: Last name is Smith with Cust1.FirstName and Cust1.LastName. Indexers allow you to do
}
internal assembly or any derived classes. The union of } the same with Cust1[0] and Cust1[1] respectively. An indexer is a pair of
protected and internal. If the class has a constructor that initializes a field, the field in the get and set accessors inside the code block of ReturnType this [
virtual – method can be overridden in subclass. object initializer syntax wins. Below, the last name is Smith. Type param1, ... ]. The set and get blocks use switch.
override – overrides virtual method in base class. public class Person
new – hides non-virtual method in base class. {
public int id { get; set; } Inheritance
sealed – prevents derived class from inheriting. public string FirstName { get; set; } Inheritance is a type of relationship (“Is-A”) between classes that allows
abstract – must be implemented by subclass. public string LastName { get; set; }
one class to inherit members from the other (code reuse). A horse “is
public DateTime BirthDate { get; set; }
Below is an example of a method called MyMethod (() public Person() // constructor an” animal. Inheritance allows for polymorphic behaviour. In UML, the
public class MyClass
{
{ LastName = "Johnson"; } Animal is at the top with the Horse under it with an arrow pointing up
}
public int MyMethod (int integer, string text) static void Main(string[] args) to Animal. Another example of inheritance is where a Saving Bank
{
return 0;
{ // don't need to initialize all fields Account and Chequing Bank Account inherit from a Bank Account.
var p = new Person {FirstName = "J", LastName = "Smith"}; public class BaseClass
} Console.WriteLine("Last name is {0}", p.LastName); {
} // OUTPUT: Last name is Smith public int Amount { get; set; }
} public BaseClass() { Console.WriteLine("Base constr"); }
Properties public void BaseDo() { Console.WriteLine("Base's BaseDo."); }
public virtual void Do() { Console.WriteLine("Base's Do"); }
A property is declared like a field, but with a get/set block added. Indexers }
Properties look like fields from the outside, but internally they contain Indexers provide a natural syntax for accessing elements in a class or public class SubClass : BaseClass
{
logic, like methods do. You can set the values of a public field and a struct that encapsulate a list or dictionary of values. Indexers are like public SubClass() { Console.WriteLine("Sub constr"); }
public property, no problem. Note that -= means subtract from self. properties but are accessed via an index argument rather than a public override void Do() { Console.WriteLine("Sub's Do");}
class Program }
property name. The string class has an indexer that lets you access each class Program
{
static void Main(string[] args) of its char values via an int Index. {
{ string s = "hello"; static void Main(string[] args)
Item it = new Item(); Console.WriteLine(s[0]); // 'h' zero-based {
it.FieldPrice = 24.67M; Console.WriteLine(s[1]); // 'e' var bas = new BaseClass();
it.PropertyPrice = 45.21M; Console.WriteLine(s[99]); // IndexOutOfRangeException var sub = new SubClass();
Console.WriteLine(it.FieldPrice + " " + it.PropertyPrice); Console.WriteLine(s[-1]); // IndexOutOfRangeException sub.Amount = 1; // Amount inherited from Base
it.FieldPrice -= 1.00M; The index argument(s) can be of any type(s), unlike arrays. You can call sub.Do(); // Sub's Do
it.PropertyPrice -= 1.00M; }
Console.WriteLine(it.FieldPrice + " " + it.PropertyPrice);
indexers null-conditionally by inserting a question mark before the }
} square bracket as shown below. Output:
} string str = null;
public class Item Console.WriteLine(str?[0]); // Writes nothing; no error. Base constr
{ Console.WriteLine(str[0]); // NullReferenceException Base constr
public decimal FieldPrice; To write an indexer, define a property called this, specifying the Sub constr
public decimal PropertyPrice { get; set; }
} arguments in square brackets. Sub's Do
class Sentence
Here is a public property Amount with its backing field, that can be {
simplified with auto implemented property with { get; set; }. string[] words = "The quick brown fox".Split(); //field Constructor Inheritance
private decimal _amount; // backing field public Sentence() { } // default constructor When you instantiate a sub class, base class constructors are always
public decimal Amount // public property public Sentence(string str) // constructor
{ { words = str.Split(); } executed first, then sub class constructors, as you can see in lines 2 and
get { return _amount; } public int Length // property 3 from the output. Base class constructors are not inherited.
set { _amount = value; } // notice the keyword value { get { return words.Length; } }
} public string this[int wordNum] // indexer
C# OOP Cheat Sheet (3 of 4) Composition vs Inheritance }
display.WriteMyMessages(baseclasses);

begincodingnow.com Designing classes needs to be done carefully. Be careful with designing }


your inheritance because it can result in large hierarchies that become You can assign a variable that is of a derived type to a variable of one of
fragile (difficult to modify due to tight coupling). You can always re- the base types. No casting is required for this. You can then call
Composition (aka Containment) methods of the base class through this variable. This results in the
factor inheritance into composition. A horse and a fish are both
Composition is a type of relationship (“has -a”) between two classes implementation of the method in the derived class being called. You
animals, but they are quite different. Both eat and sleep (Animal class)
that allows one class to contain another. Inheritance is another type of can cast a base type variable into a derived class variable and call the
but horses walk and fish swim. You could use composition and create a
relationship. Both methods give us code re-use. In our example, both
CanWalk and CanSwim class. The horse “has-a” CanWalk class. This is method of the derived class.
the car and truck have an engine and the engine needs to send a
fine even though “has-a” may not make sense or sound correct in the
message to the console. We use a private field in the composite class
(car and truck) to achieve this. You use a member field to hold an object
real world. You don’t want to put a Walk() method in your Animal class Interfaces
unless you are certain all of your animals now and in the future can An interface is like a class, but it provides a specification rather than an
instance. Generally, inheritance results in a more tightly-couple
walk. If you have that, using inheritance, you may need a sub-class of implementation for its members. It’s a “contract”. Interface members
relationship than composition and many developers prefer
Animal called mammal, and re-compile and re-deploy your code. Also, are all implicitly abstract. A class (or struct) can implement multiple
composition, but it depends on your project. Two things to remember:
with composition we get an extra benefit that’s not possible with interfaces. In contrast, a class can inherit from only a single class, and a
private field and constructor.
inheritance: Interfaces. We can replace our Animal class with an struct cannot inherit at all (aside from deriving from
using System; interface IAnimal. This is dependency injection and is covered later in System.ValueType). The interface's members will be implemented
namespace CompositionGeneral the topic called Interfaces & Extensibility. by the classes and structs that implement the interface. By convention,
{
class Car interface names start with the capital letter “I”.
{ Method Overriding & Polymorphism
private readonly Engine _engine; interface IInfo
public Car(Engine engine) // constructor
Method overriding is changing the implementation of an inherited
{
{ method that came from the base class. Use the virtual keyword in string GetName();
_engine = engine; the method of the base class and override in the derived class. string GetAge();
} }
public void DriveCar() Virtual is just the opportunity to override. You don’t have to class CA : IInfo
{ override it. What is polymorphism? Poly means many and morph { // declare that CA implements the interface IInfo
float speed = 0.0F; public string Name;
_engine.EngineStatus("car starting engine");
means form. Let’s use an example with classes called BaseClass,
public int Age;
speed = 50.0F; ChildRed and ChildBlue. // implement two interface methods of IInfo:
class MyBaseClass public string GetName() { return Name; }
{ public string GetAge() { return Age.ToString(); }
public int CommonProperty { get; set; } }
_engine.EngineStatus($"speed of {speed} Km/hr");
public virtual void WriteMessage() { } class CB : IInfo
_engine.EngineStatus("car engine off");
} { // declare that CB implements the interface
}
class ChildRed : MyBaseClass public string First;
}
{ public string Last;
class Truck
public new int CommonProperty { get; set; } public double PersonsAge;
{
public override void WriteMessage() public string GetName() { return First + " " + Last; }
private readonly Engine _engine;
{ public string GetAge() { return PersonsAge.ToString(); }
public Truck(Engine engine) // constructor
CommonProperty = 46; }
{
Console.WriteLine("Red " + CommonProperty); } class Program
_engine = engine;
} { // pass objects as references to the interface
}
class ChildGreen : MyBaseClass static void PrintInfo(IInfo item)
public void DriveTruck() { //...
{ {
}
public override void WriteMessage() Console.WriteLine("Name: {0} Age: {1}", item.GetName() ,
}
{ item.GetAge() );
class Engine // the car and truck "Have An" engine
Console.WriteLine("Green " + CommonProperty); }
{
} static void Main()
public void EngineStatus(string message)
} {
{
class Display // instantiate using object initialization syntax
Console.WriteLine("Engine status: " + message);
{ CA a = new CA() { Name = "John Doe", Age = 35 };
}
public void WriteMyMessages(List<MyBaseClass> baseclasses) CB b = new CB() { First = "Jane", Last = "Smith",
}
{ PersonsAge = 44.0 };
class Program
foreach (var bc in baseclasses) // references to the objects are automatically
{
{ // converted to references
static void Main(string[] args)
bc.WriteMessage(); // to the interfaces they implement (in the code below)
{
} PrintInfo(a);
var e = new Engine();
} PrintInfo(b);
var sedan = new Car(e);
}
sedan.DriveCar();
var pickup = new Truck(new Engine()); When we call WriteMessage() above we have polymorphic behavior. Type myType = typeof(Program);
Console.Title = myType.Namespace;
pickup.DriveTruck(); We have a list of different colors, but the implementation is different }
} for each colour. Red and Green overrode the base class’s method. }
} Notice that the list is a list of the base class MyBaseClass. Output:
} class Program Name: John Doe Age: 35
Instead of having the car and truck contain a concrete class, like Engine, {
Name: Jane Smith Age: 44
static void Main(string[] args)
what if we used an interface, like IEngine instead? Please see the {
section on Interfaces & Extensibility for an example of this. var baseclasses = new List<MyBaseClass>();
baseclasses.Add(new ChildRed() { CommonProperty = 1 });
baseclasses.Add(new ChildGreen() { CommonProperty = 3 });
var display = new Display();
C# OOP Cheat Sheet (4 of 4) Interfaces & Testability Here is our test. We need to test the JournalPoster’s Post method. We
need to isolate it so we can write code to test our code. Go to the
begincodingnow.com Using interfaces help with unit testing. This example builds on many
topics in this cheat sheet. We’ll use the Microsoft Test Runner. You get Solution Explorer ➤ Right-Click Solution ➤ Add ➤ Project ➤ Visual
a new journal entry and then post it passing the entry to the Post() C# ➤ Test ➤ Unit Test Project ➤ Name it after the Project and
Interfaces & Extensibility
method of the JournalPoster class. Posting the entry requires the append .UnitTests to the name ➤ OK. You get the following by default.
We create a constructor and inject a dependency. This is called
services of the checker. In order to make testing work, we need to use We’ll change that.
dependency injection, which means that in the constructor we are
an interface for the checker. [TestClass]
specifying the dependencies of our class. The FileProcessor is not class Program public class UnitTest1
directly dependent on the ConsoleLogger. It doesn’t care who { {
static void Main(string[] args) [TestMethod]
implements ILogger. It could be a DatabaseLogger that does it. public void TestMethod1()
{
// FilesProcessor is dependent on an interface. {
var jp = new JournalPoster(new DrEqualsCrChecker());
public interface ILogger }
var je = new JournalEntry { DebitAmount = 120.50f,
{ }
CreditAmount = -120.50f };
void LogError(string message); // method
void LogInfo(string message); // method
jp.Post(je); Since we want to test the JournalPoster(),rename UnitTest1 to
Console.WriteLine("Posted? " + je.IsPosted); JournalPosterTests. Rename TestMethod1 following the naming
}
Console.WriteLine("Date posted: " +
public class ConsoleLogger : ILogger
je.Posting.PostingDate.ToString("yyyy-MM-dd")); convention of methodname_condition_expectation. We need to add a
{ // ConsoleLogger implements ILogger
public void LogError(string message)
Console.WriteLine("Debit amount: {0:C}", je.DebitAmount); Reference to our Project in our UnitTest project. In the unit test
project, Right-Click References ➤ Add Reference ➤ Projects ➤ click
Console.WriteLine("Credit amount: {0:C}", je.CreditAmount);
{
Console.WriteLine("JE Balance: {0:C}", je.Posting.Balance);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message);
} the check box of the project. We need to create a Fake debit equals
}
Console.ForegroundColor = ConsoleColor.White; credit checker. Why? We don’t want to pass the original one to the
} Output: JournalPoster. We need to pass a fake one that must always be working
public void LogInfo(string message) Posted? True
{ because our testing is focussing on the JournalPoster, not the checker.
Console.ForegroundColor = ConsoleColor.Green;
Date posted: 2019-02-07 [TestClass]
Console.WriteLine(message); Debit amount: $120.50 public class JournalPosterTests
Console.ForegroundColor = ConsoleColor.White; Credit amount: -$120.50 {
} // need to add a Reference to our project
}
JE Balance: $0.00 [TestMethod]
public class FilesProcessor [ExpectedException(typeof(InvalidOperationException))]
{ public class JournalEntry public void JournalPoster_JEIsAlreadyPosted_ThrowsAnException()
private readonly ILogger _logger; { // in the reaal world there is more than this { // naming convention: methodname_condition_expection
public FilesProcessor(ILogger logger) // constructor public Posting Posting { get; set; } var JournalPoster = new JournalPoster(new FakeDrEqualsCrChecke
{ public float DebitAmount = 0f; r());
_logger = logger; public float CreditAmount = 0f; var je = new JournalEntry { Posting = new Posting() };
} public DateTime DatePosted { get; set; } JournalPoster.Post(je);
public void Process() public bool IsPosted }
{ { [TestMethod]
try get { return Posting != null; } public void JournalPoster_JEIsNotPosted_ShouldSetPostedPropertyOfJ
{ // we might employ the using keyword in the real world } ournalEntry()
_logger.LogInfo($"Migrating started at {DateTime.Now}"); } {
_logger.LogInfo($"In middle of doing stuff..."); public class Posting var JournalPoster = new JournalPoster(new FakeDrEqualsCrChecke
int zero = 0; { r());
int myError = 1 / zero; public float Balance { get; set; } // zero if Dr = Cr var je = new JournalEntry();
_logger.LogInfo($"Migrating ended at {DateTime.Now}"); public DateTime PostingDate { get; set; } JournalPoster.Post(je);
} } Assert.IsTrue(je.IsPosted);
catch public class JournalPoster Assert.AreEqual(1, je.Posting.Balance);
{ { // this class does not even know about DrEqualsCrChecker Assert.AreEqual(DateTime.Today.AddDays(1), je.Posting.PostingD
_logger.LogError($"Opps! Error"); private readonly IDrEqualsCrChecker _checker; ate);
} public JournalPoster(IDrEqualsCrChecker checker) }
} { _checker = checker; } }
} public void Post(JournalEntry je) public class FakeDrEqualsCrChecker : IDrEqualsCrChecker
class Program { { // methods defined in an interface must be public
{ if (je.IsPosted) public float CalcBalance(JournalEntry je)
static void Main(string[] args) throw new InvalidOperationException("Opps. Already posted! { return 1; } // simple and it will works
{ "); }
// Our logger to sends to console je.Posting = new Posting
var filesProcessor = new FilesProcessor(new ConsoleLogger()); {
filesProcessor.Process(); Balance = _checker.CalcBalance(je),
Console.WriteLine("done program."); PostingDate = DateTime.Today.AddDays(1)
} };
} }
Output: }
public interface IDrEqualsCrChecker
Migrating started at 2019-02-07 10:05:43 AM { float CalcBalance(JournalEntry je); }
In middle of doing stuff... public class DrEqualsCrChecker : IDrEqualsCrChecker
Opps! Error {
public float CalcBalance(JournalEntry je)
done program. {
var balance = je.DebitAmount + je.CreditAmount;
We can extend it. We can create more loggers other than return balance;
}
ConsoleLogger, such as DatabaseLogger, FileLogger, EmailLogger }
SMSLogger and so on, and all we need to do is change the Main().
C# Advanced Cheat Sheet (1 of 4) { // mix
public
fields
int Id
with a property just for demonstration
= 0; }
}

begincodingnow.com public string Name = ""; Our code has 3 methods that act upon the above class. They are:
public string Status { get; set; }
} AddOne(), DoubleIt() and AppendString().
class MyClassMethods
Generics static void Main(string[] args)
{ {
C# has two separate mechanisms for writing code that is reusable var customers = new List<Customer> public void AddOne(MyClass mc)
{ // here we do something with the object mc
across different types: inheritance and generics. Whereas inheritance { // using object initialization syntax here
mc.MyInt = mc.MyInt + 1;
new Customer { Id = 4, Name = "Jack", Status = "Active"},
expresses re-usability with a base type, generics express reusability new Customer { Name = "Sally", Status = "Active"} Console.WriteLine("AddOne: " + mc.MyString + " " + mc.MyInt);
with a "template" that contains "placeholder" types. }; }
using System; customers.Add(new Customer { Name = "Sam" }); public void DoubleIt(MyClass mc)
using System.Collections.Generic; foreach (Customer cust in customers) {
namespace Generics { Console.WriteLine(cust.Id + " " + cust.Name + mc.MyInt = mc.MyInt * 2;
{ " " + cust.Status); } Console.WriteLine("DoubleIt:" + mc.MyString + " " + mc.MyInt);
public class Customer } }
{ } public void AppendString(MyClass mc)
public int Id { get; set; } {
public string Name { get; set; } mc.MyString = mc.MyString + " appending string now ";
} Delegates Console.WriteLine("AppendString: " + mc.MyString + " "
+ mc.MyInt);
class Program A delegate is an object that “holds” one or more methods. A delegate is }
{
static void Main(string[] args) a reference to a function or ordered list of functions with a specific }
class MyClassProcessor
{ signature. You can “execute” a delegate and it will execute the method {
List<Customer> myCustomers = new List<Customer>(); //empty
myCustomers.Add(new Customer() { Id = 1, Name = "Jack" });
or methods that it “contains” (points to). A delegate is a user-defined public int MyAmount { get; set; }
myCustomers.Add(new Customer() { Id = 2, Name = "Jill" }); reference type, like a class. You can create your own delegate or use public delegate void MyClassMethodHandler(MyClass myclass);
foreach (Customer cust in myCustomers) { the generic ones: Func<> and Action<>. First, we’ll create our own. public void Process(MyClassMethodHandler methodHandler)
Console.WriteLine(cust.Name); } class Program
} { // methodHandler is a delegate
{ // instantiate with object initialization syntax
} delegate int Multiplier(int x); // type declaration
} var myclass = new MyClass { MyString = "In Process method ",
static void Main() MyInt = 1 };
{ methodHandler(myclass);
Multiplier t = Cube; // Create delegate instance
Lists of Objects // by assigning a method to a delegate variable.
// we do not define the methods we want to run here because
// we are going to let the consumer define that.
Lists were covered briefly in the Basics section of this cheat sheet int result = t(2); // Invoke delegate: t(3) }
Console.WriteLine(result); // 8
series, but the example was only a list of integers. Here was have a list }
}
class Program
of our own objects based on our own class: Customer. static int Cube(int x) => x * x * x; {
class Customer } static void Main(string[] args)
{ Here is second example. {
public int Id = 0; using System; var myclassprocessor = new MyClassProcessor();
public string Name = ""; namespace ReturnValues var myclassmethods = new MyClassMethods();
public string Status { get; set; } { MyClassProcessor.MyClassMethodHandler
} // Illustrated C# 7 Fifth Edition page 361 methodHandler = myclassmethods.AddOne;
class Repository delegate int MyDel(); // Declare delegate with return value. // MyClassMethodHandler is a delegate (multicast)
{ class MyClass // methodHandler is pointer to a group of functions (delegate)
private static List<Customer> privatecust = new List<Customer>(); { methodHandler += myclassmethods.DoubleIt;
public static IEnumerable<Customer> Customers { private int IntValue = 5; methodHandler += FromConsumerMinusThree;
get { return privatecust; } public int Add2() { IntValue += 2; return IntValue; } methodHandler += myclassmethods.AppendString;
} public int Add3() { IntValue += 3; return IntValue; }
public static void AddCustomers(Customer customer) { } // Process() takes a delegate
privatecust.Add(customer); class Program myclassprocessor.Process(methodHandler);
} { }
public static int NumberOfCustomers { static void Main() static void FromConsumerMinusThree(MyClass myC)
get { return privatecust.Count; } { {
} MyClass mc = new MyClass(); myC.MyInt = myC.MyInt - 3;
} MyDel mDel = mc.Add2; // Create initialize delegate. Console.WriteLine("FromConsumerMinusThree: " + myC.MyString +
class Program mDel += mc.Add3; // Add a method. myC.MyInt);
{ mDel += mc.Add2; // Add a method. }
static void Main(string[] args) Console.WriteLine($"Value: { mDel() }"); // output 12 }
{
var cust1 = new Customer { Id = 1, Name = "Joe",
} Output:
} AddOne: inside Process method 2
Status = "Active" }; }
var cust2 = new Customer { Id = 1, Name = "Sally", DoubleIt: inside Process method 4
Status = "Active" }; Here is a more realistic example of delegates. Here we create a FromConsumerMinusThree: inside Process method 1
Repository.AddCustomers(cust1); multicast delegate. The consumer of our code is the method Main(). AppendString: inside Process method appending string now 1
Repository.AddCustomers(cust2);
foreach (Customer cust in Repository.Customers)
We have an object that we need to “process” with several methods in
{ Console.WriteLine($"Name: {cust.Name} Id: {cust.Id} " + order, and we also want the code to be extensible so the consumer can Func<> and Action<>
$"Status: {cust.Status}"); } add their own methods in Main() to the list of our methods. In .NET we have 2 delegates that are generic: Action<> and Func<>.
Console.WriteLine($"Number of customers: " +
$"{Repository.NumberOfCustomers}");
class MyClass Each also come in a non-generic form. Modifying the above program
{
} public string MyString { get; set; } requires us to use Action<> and introducing a new processor (we’ll
} public int MyInt { get; set; } call it MyClassGenericProcessor) and removing our custom
Here is another example of a list of objects, without the Repository. delegate in there and adding Action<>. Also in the Main() program
class Program public static MyClass MyClassDoMethod()
{ { we need to change the first line and the third line of code.
public class Customer return new MyClass(); // we don't use these
C# Advanced Cheat Sheet (2 of 4) static void Main(string[] args)
{
public class ItemEventArgs : EventArgs
{
begincodingnow.com MyDel AddTwo = x => x + 2; public Item Item { get; set; }
Func<int, int> AddThree = number => number + 3; }
Console.WriteLine(AddOne(0)); public class ItemProcessor
Func<> and Action<> continued… Console.WriteLine(AddTwo(0)); {
class MyClassGenericProcessor Console.WriteLine(AddThree(0)); // public delegate void ItemProcessedEventHandler(object source,
{ } // ItemEventArgs args);
public int MyAmount { get; set; } static int AddOne(int number) public event EventHandler<ItemEventArgs> ItemProcessed;
// public delegate void MyClassMethodHandler(MyClass myclass); {
return number + 1; public void ProcessItem(Item item)
public void Process(Action<MyClass> methodHandler) } {
{ // methodHandler is a delegate } Console.WriteLine("Processing Item...");
// instantiate with object initialization syntax Here is another example. Thread.Sleep(1500); // delay 1.5 seconds
var myclass = new MyClass { MyString = "in Process method ", static void Main(string[] args) OnItemProcessed(item);
MyInt = 1 }; { }
methodHandler(myclass); Console.WriteLine(Square(3)); // 9 protected virtual void OnItemProcessed(Item item)
// we do not define the methods we want to run here because Func<int, int> squareDel = Square; {
// we are going to let the consumer define that. Console.WriteLine(squareDel(3)); // 9 ItemProcessed?.Invoke(this, new ItemEventArgs() { Item = item
} Func<int, int> squareLambda = m => m * m; });
} Console.WriteLine(squareLambda(3)); // 9 // if (ItemProcessed != null)
// ItemProcessed(this, new ItemEventArgs() { Item = item });
Below is a partial listing of our Main() program showing the changes. Func<int, int, long> multiplyTwoInts = (m, n) => m * n;
}
var myclassprocessor = new MyClassGenericProcessor(); // generics Console.WriteLine(multiplyTwoInts(3,4)); // 12
} }
var myclassmethods = new MyClassMethods(); public class SubscriberOne
Action<MyClass> methodHandler = myclassmethods.AddOne; static int Square(int number)
{ {
return number * number; public void OnItemProcessed(object source, ItemEventArgs args)
{ // maybe send an email
Anonymous Types }
Console.WriteLine("SubscriberOne: " + args.Item.Name);
An anonymous type is a simple class created on the fly to store a set of Here is another example that is more realistic. Here we have a list of }
values. To create an anonymous type, you use the new keyword Products. We also have a repository of products. We use object }
class SubscriberTwo
followed by an object initializer { }, specifying the properties and values initialization syntax to initialize the list with a series of products. {
the type will contain. Anonymous types are used in LINQ queries. FindAll() takes a predicate. A predicate is something that evaluates public void OnItemProcessed(object source, ItemEventArgs args)
static void Main(string[] args) to true or false. { // maybe send SMS (text message)
{ class Product Console.WriteLine("SubscriberTwo: " + args.Item.Name);
var person = new { Name = "Bob", Number = 32 }; { }
Console.WriteLine($"Name: {person.Name} " + public string Title { get; set; } }
$"Number: {person.Number}"); public int Price { get; set; } Here is the main program.
// output: Name: Bob Number: 32 } class Program
} {
Here is another example. class ProductRepository static void Main(string[] args)
class Program { {
{ public List<Product> GetProducts() var item = new Item() { Name = "Item 1 name" };
static void Main(string[] args) { var itemProcessor = new ItemProcessor(); // publisher
{ return new List<Book> var subscriberOne = new SubscriberOne(); // subscriber
var person = new { var subscriberTwo = new SubscriberTwo(); // subscriber
{ new Product () { Title ="product 1", Price = 5},
Name = "John", new Product () { Title = "product 2", Price = 6 }, Console.WriteLine("Beginning program EventsExample...");
Age = 29, new Product () { Title = "product 3", Price = 17 }
Major = "Computers" }; // itemProcessed is a list of pointers to methods
}; } itemProcessor.ItemProcessed += subscriberOne.OnItemProcessed;
Console.WriteLine($"{ person.Name }, Age { person.Age }, " } itemProcessor.ItemProcessed += subscriberTwo.OnItemProcessed;
+ $"Major: {person.Major}"); class Program
// the code below produces the same results { itemProcessor.ProcessItem(item);
string Major = "Computers"; static void Main(string[] args) }
var guy = new { Age = 29, Other.Name, Major }; { }
Console.WriteLine($"{guy.Name }, Age {guy.Age }, " var products = new ProductRepository().GetProducts();
List<Product> cheapProducts = products.FindAll(b =>
+ $"Major: {guy.Major}");
b.Price < 10); Attributes
// John, Age 29, Major: Computers
} foreach (var product in cheapProducts) Attributes allow you to add metadata to a program’s assembly.
{
}
Console.WriteLine(product.Title + " $" + product.Price);
Attribute names use Pascal casing and end with the suffix Attribute. An
class Other
{ } attribute section consists of square brackets enclosing an attribute
// Name is a static field of class Other } name and sometimes a parameter list. A construct with an attribute
}
static public string Name = "John"; applied to it is said to be decorated, or adorned, with the attribute. Use
} You can use a lambda expression when argument requires a delegate.
the [Obsolete] attribute to mark the old method as obsolete and to
display a helpful warning message when the code is compiled.
Lambda Events
A lambda expression is an unnamed method written in place of a 1. define a delegate (define signature) or use EventHandler<>
delegate instance. A lambda expression is an anonymous method that
Preprocessor Directives
2. define an event based on that delegate (ItemProcessed in this case)
C# includes a set of preprocessor directives that are mainly used for
has no access modifier, no name and no return statement. We have 3. raise the event
conditional compilation. The directives #region and #endregion
code below that we can re-factor using a lambda expression. The => is Here is an example program that uses events.
delimit a section of code that can be expanded or collapsed using the
read as “goes to”. public class Item
class Program { outlining feature of Visual Studio and can be nested within each other.
{ public string Name { get; set; } // a property
delegate int MyDel(int InParameter); // custom delegate }
C# Advanced Cheat Sheet (3 of 4) {
public IEnumerable<Product> GetProducts() // method
database with a column like MiddleName or BirthDate which may have
a null value.
begincodingnow.com {
return new List<Product> static void Main(string[] args)
{ {
// DateTime is a value type - cannot be null, but...
Extension Methods new Product() {Name = "P one", Price = 5},
new Product() {Name = "P two", Price = 9.99f}, System.Nullable<DateTime> d = null;
Extension methods allow an existing type to be extended with new new Product() {Name = "P three", Price = 12}, DateTime? dt = null;
Console.WriteLine("GetValueOrDefault: " + dt.GetValueOrDefault());
methods, without altering the definition of the original type. An };
Console.WriteLine("HasValue: " + dt.HasValue); // property
}
extension method is a static method of a static class, where the this } // below line causes InvalidOperationException when null
modifier is applied to the first parameter. The type of the first class Program // Console.WriteLine("Value: " + dt.Value); // property
{ Console.WriteLine(dt);
parameter will be the type that is extended. Extension methods, like static void Main(string[] args)
instance methods, provide a way to chain functions. { // output: 0001-01-01 12:00:00 AM
public static class MyStringExtensions var products = new ProductRepository().GetProducts(); // output: False
{ var pricyProducts = new List<Product>(); // output:
public static string Shorten(this String str, int numberOfWords) // ------without LINQ---------------------------- }
{ foreach (var product in products) What about conversions and the null-coalescing operator?
if (numberOfWords < 0) throw new { // Conversions
ArgumentOutOfRangeException("must contain words"); if (product.Price > 10) DateTime? date = new DateTime(2019, 1, 1);
if (numberOfWords == 0) return ""; pricyProducts.Add(product); // DateTime date2 = date; compiler says cannot convert
string[] words = str.Split(' '); } DateTime date2 = date.GetValueOrDefault();
if (words.Length <= numberOfWords) return str; // ------without LINQ----------------------------- Console.WriteLine("date2: " + date2);
return string.Join(" ", words.Take(numberOfWords)) + "..."; foreach (var product in pricyProducts) DateTime? date3 = date2;
} Console.WriteLine("{0} {1:C}",product.Name, product.Price); Console.WriteLine(date3.GetValueOrDefault());
} }
class Program } // Null Coales Operator: ??
{ When you type product followed by the dot, Intelisense gives you a few DateTime? date4 = null;
static void Main(string[] args) DateTime date5;
{ methods and a long list of extension methods. One extension method is // if date has a value use that, otherwise use today
string senten = "A very very long sentence..."; Where<>. Where is asking for a delegate. Func<Product,bool> if (date4 != null)
Console.WriteLine("Number of chars: " + senten.Length);
var shortededSentence = senten.Shorten(10);
predicate. It points to a method that gets a Product and returns a date5 = date4.GetValueOrDefault();
else
var s2 = shortededSentence.ToUpper(); bool based on the predicate. Whenever we see Func<> as a delegate date5 = DateTime.Today;
var s3 = s2.PadRight(60); we can use a Lambda expression such as p => p.Price > 10. Here is the // null
Console.WriteLine("[" + s3 + "]"); date5 = date4 ?? DateTime.Today; // same as if block above
} code with LINQ.
} // -----with LINQ------------------------------------------- When working with nullable types, GetValueOrDefault() is the
var pricyProducts2 = products.Where(p => p.Price > 10); preferred way of doing things.
// -----with LINQ-------------------------------------------
LINQ The LINQ extension methods can be chained. When we use Select in
LINQ stands for Language Integrated Query and is pronounced “link.” this case we get back a list of strings, not products. Dynamics
LINQ is an extension of the .NET Framework and allows you to query // -----LINQ------------------------------------------ Programming languages are either static or dynamic. C# and Java are
collections of data in a manner like using SQL to query databases. With
var pricyProducts2 = products.Where(p => p.Price > 8) static, but Ruby, JavaScript and Python are dynamic. With static
.OrderBy(p => p.Name)
LINQ you can query data from databases (LINQ to Entities), collections .Select(p => p.Name); // string languages the types are resolved at compile time, not at run time. The
of objects in memory (LINQ to Objects), XML documents (LINQ to XML), // -----LINQ------------------------------------------ CLR (.NET’s virtual machine) takes compiled code (verified by the
foreach (var product in pricyProducts2)
and ADO.NET data sets (LINQ to Data Sets). Console.WriteLine(product);
compiler) which is in Intermediate language (IL) and converts that to
using System; There are several LINQ extension methods beyond Where(). A few are machine code at runtime. Runtime checking is performed by the CLR.
using System.Collections.Generic;
listed in the C# comments below. If you only want one Product you can Runtime type checking is possible because each object on the heap
using System.Linq;
namespace LINQint use Single() or SingleOrDefault(). Single() will throw an internally stores a little type token. You can retrieve this token by
{
error InvalidOperationException if it can’t find a match. The OrDefault calling the GetType method of object (reflection). With C# dynamics
class Program
{ will return null if it can’t find a match, which is probably better. and the keyword dynamic, we don’t need to use reflection. Much
static void Main(string[] args) cleaner code results. When converting from dynamic to static types, if
{
var product = products.Single(p => p.Name == "P two"); the runtime type of the dynamic object can be implicitly converted to
int[] numbers = { 6, 47, 15, 68, 23 }; // Data source
IEnumerable<int> bigNums = // Define & store the query.
var product2 = products.SingleOrDefault(p => p.Name == "P unknown"); the target type we don’t need to cast it.
Console.WriteLine(product.Name); // P two dynamic name = "Bob";
from n in numbers
Console.WriteLine(product2 == null); // output: True name = 19; // this works because name is dynamic!
where n > 30
var product3 = products.First(); name++;
orderby n descending
Console.WriteLine(product3.Name); // P one Console.WriteLine(name); // 20
select n;
// FirstOrDefault() Last() LastOrDefult() dynamic a = 4, b = 5;
foreach (var x in bigNums) // Execute the query.
// Skip(2).Take(3) will skip the first 2 and take the next 3 var c = a + b; // c becomes dynamic
Console.Write($"{ x }, "); // output: 68, 47 // Count() Max() Min() Sum() Average() Console.WriteLine(c); // 9
}
// Average(p => p.Price) int i = 7;
}
} dynamic d = i;
long l = d;
Now let’s use a more realistic example. First we’ll show the code Nullable Types Console.WriteLine(l); //
without LINQ, then with LINQ. We have a class of our objects called Reference types can represent a nonexistent value with a null
Product and we have a ProductRepository. reference. Normally value types cannot be null, however to represent
class Product
{
null in a value type, you must use a special construct called a nullable
public string Name { get; set; } type which is denoted with a value type immediately followed by the ?
public float Price { get; set; } symbol. An important use case for nullable types is when you have a
}
class ProductRepository
} // hierarchy & click parent (bottom right)
public async Task<string> GetHtmlAsync(string url) }
C# Advanced Cheat Sheet (4 of 4) {
var webClient = new WebClient();
catch (DivideByZeroException ex)
{
Asynchronous continued… return await webClient.DownloadStringTaskAsync(url); Console.WriteLine("Cannot divide by zero. " + ex.Message);
} }
catch (ArithmeticException ex)
Asynchronous {
In the synchronous model the program executes line by line, but in the Exception Handling }
Console.WriteLine("Arithmetic exception. " + ex.Message);

asynchronous model (e.g. media players, web browsers), We write exception handling code to avoid those Unhandled Exception catch (Exception ex)
responsiveness is improved. In .NET 4.5 (in 2012) Microsoft introduced messages when the program crashes. We can use a Try Catch block. {
Console.WriteLine("Unexpected error! " +
a new a new asynchronous model, instead of multi-threading and call- The four keywords of exception handling are: try, catch, finally and ex.Message); // F9, F5
backs. It uses async and await keywords. In our example we have a throw. The first code example crashes with an unhandled exception. In // Unexpected error! Attempted to divide by zero.
}
WPF program that has 2 blocking operations (downloading and the second example we handle the exception. finally // unmanaged resources are not handled by CLR
writing). You can only use the await operator inside an async public class Calculator { } // we need to .Dispose() of those here, unless we employ
{ // the using statement.
method. Async affects only what happens inside the method and has public int Divide(int numerator, int denominator) }
no effect on a method’s signature or public metadata. { class Program
return numerator / denominator; { // we need using System.IO;
} static void Main(string[] args)
using System.IO;
} {
using System.Net;
class Program try
using System.Threading.Tasks;
{ { // using creates finally block in background
using System.Windows;
static void Main(string[] args) using (var strmRdr = new StreamReader(@"c:\not.txt")) ;
namespace AysnchronousProgramming
{ }
{
var calc = new Calculator(); catch (Exception ex)
public partial class MainWindow : Window
// Unhandled Exception: System.DivideByZeroException: {
{
// Attempted to divide by zero. CRASHES !! Console.WriteLine("Unexpected error!");
public MainWindow()
var result = calc.Divide(89, 0); }
{
} }
InitializeComponent();
} }
}
private async void Button_Click(object sender, Let’s refactor our Main() method to use a try catch block. One of the new features in C# 6 was exception filters, which are not
RoutedEventArgs e) static void Main(string[] args)
{ { covered here. They give you more control over your catch blocks and
await DownloadHtmlAsync("http://begincodingnow.com"); try further tailor how you handle specific exceptions.
} {
public async Task DownloadHtmlAsync(string url) var calc = new Calculator();
{ // decorate method async, use Task, and only by convention var result = calc.Divide(89, 0); Recursion
// put "Async" at end of the method name. }
catch (Exception ex)
Recursion happens when a method or function calls itself. We must
var webClient = new WebClient();
// use TaskAsync not Async, and await is a compiler marker { write a condition that checks that the termination condition is satisfied.
var html = await webClient.DownloadStringTaskAsync(url); Console.WriteLine("Unexpected error!"); // F9, F5 Below is a program that tells you how many times a number is evenly
using (var streamWriter = new }
StreamWriter(@"c:\temp\result.html")) } divisible by a divisor.
public static int CountDivisions(double number, double divisor)
{ // use the Async one: WriteAsync and add await To implement multiple catch blocks set a break point (with F9) and run {
await streamWriter.WriteAsync(html);
}
it in debug mode (F5). Place your cursor on “ex” and click the small int count = 0;
MessageBox.Show("Finished downloading","Asynch Example"); right-arrow icon in the pop-up to bring up more details. Properties have if (number > 0 && number % divisor == 0)
{
} the wrench icon. Look at Message, Source (the DLL or assembly), count++;
public void DownloadHtml(string url)
{ // NOT asynchronous! - just shown here for comparison StackTrace (sequence of method calls in the reverse order – click the number /= divisor;
return count += CountDivisions(number, divisor);
var webClient = new WebClient(); magnifier icon), TarketSite (method where exception happened) and }
var html = webClient.DownloadString(url);
the others. return count;
static void Main(string[] args) }
using (var streamWriter = new
{ static void Main(string[] args)
StreamWriter(@"c:\temp\result.html"))
try {
{
{ Console.WriteLine("Enter your number: ");
streamWriter.Write(html);
var calc = new Calculator(); double number = Convert.ToDouble(Console.ReadLine());
}
var result = calc.Divide(89, 0); Console.WriteLine("Enter your divisor: ");
}
} double divisor = Convert.ToDouble(Console.ReadLine());
}
catch (Exception ex) int count = CountDivisions(number, divisor);
}
{ Console.WriteLine($"Total number of divisions: {count}");
Now we will modify our program. The message box “Waiting…” Console.WriteLine("Unexpected error! " + Console.ReadKey();
executes immediately. We can execute other code here. Another ex.Message); // F9, F5 }
// Unexpected error! Attempted to divide by zero.
message box executes after the blocking operation completes. }
private async void Button_Click(object sender, RoutedEventArgs e) }
{
//await DownloadHtmlAsync("http://begincodingnow.com"); Multiple catch blocks example below.
// Note: if we use await we must use async in method definition. static void Main(string[] args)
// var html = await GetHtmlAsync("http://begincodingnow.com"); {
var getHtmlTask = GetHtmlAsync("http://begincodingnow.com"); try
// executes immediately {
MessageBox.Show("Waiting for task to complete..."); var calc = new Calculator();
var html = await getHtmlTask; var result = calc.Divide(89, 0);
// executes after html is downloaded // type DivideByZeroException and F12
MessageBox.Show(html.Substring(0, 500)); // for Object Browser to see inheritance
Another random document with
no related content on Scribd:
Carolina convention, the same Iredell, after pointing out that the
American concept of the relation of citizen to all governments had
become basic American law, contrasts that fact with the fundamental
law of Great Britain where “Magna Charta itself is no constitution, but
a solemn instrument ascertaining certain rights of individuals, by the
legislature for the time being; and every article of which the
legislature may at any time alter.” (4 Ell. Deb. 148.)
In the Pennsylvania convention, on December I, 1787, one of the
most distinguished lawyers of that generation made a memorable
speech, expressing the universal knowledge that the American
concept had taken forever the place of the Tory concept in
fundamental American law. We commend a careful study of that
speech to those of our public leaders and “constitutional” lawyers,
who for five years have been acting on the assumption that the Tory
concept has again become our fundamental American law. We
average Americans, after living with those earlier Americans, are not
surprised to listen to the statements of Wilson. “The secret is now
disclosed, and it is discovered to be a dread, that the boasted state
sovereignties will, under this system, be disrobed of part of their
power.... Upon what principle is it contended that the sovereign
power resides in the state governments?... The proposed system
sets out with a declaration that its existence depends upon the
supreme authority of the people alone.... When the principle is once
settled that the people are the source of authority, the consequence
is, that they may take from the subordinate governments powers
which they have hitherto trusted them, and place those powers in the
general government, if it is thought that there they will be productive
of more good. They can distribute one portion of power to the more
contracted circle, called state governments; they can also furnish
another proportion to the government of the United States. Who will
undertake to say, as a state officer, that the people may not give to
the general government what powers, and for what purposes, they
please? How comes it, sir, that these state governments dictate to
their superiors—to the majesty of the people?” (2 Ell. Deb. 443.)
We average Americans, legally bound (as American citizens) by
no command (interfering with our human freedom) except from our
only legislature at Washington and then only in those matters in
which we ourselves, the citizens of America, have directly given it
power to command us, now intend insistently to ask all our
governments, the supreme one at Washington and the subordinate
ones in the states of which we are also citizens, exactly the same
question which Wilson asked.
Daniel Webster asked almost exactly the same question of Hayne
and history does not record any answer deemed satisfactory by the
American people. Webster believed implicitly in the concept of
American law stated by those who made our Constitution. Like them,
and unlike our “constitutional” lawyers, he knew that the Tory
concept of the relation of men to their government had disappeared
from American basic law.
“This leads us to inquire into the origin of this government, and the
source of its power. Whose agent is it? Is it the creature of the state
legislatures, or the creature of the people?... It is, sir, the people’s
constitution, the people’s government—made for the people, made
by the people, and answerable to the people. The people of the
United States have declared that this Constitution shall be the
supreme law. We must either admit the proposition, or dispute their
authority. The states are, unquestionably, sovereign, so far as their
sovereignty is not affected by this supreme law. But the state
legislatures, as political bodies, however sovereign, are yet not
sovereign over the people.... The national government possesses
those powers which it can be shown the people have conferred on it,
and no more.... We are here to administer a Constitution emanating
immediately from the people, and trusted by them to our
administration.... This government, sir, is the independent offspring
of the popular will. It is not the creature of state legislatures; nay,
more, if the whole truth must be told, the people brought it into
existence, established it, and have hitherto supported it, for the very
purpose, amongst others, of imposing certain salutary restraints on
state sovereignties.... The people, then, sir, erected this government.
They gave it a constitution, and in that constitution they have
enumerated the powers which they bestow upon it.... Sir, the very
chief end, the main design for which the whole constitution was
framed and adopted, was to establish a government that should not
be obliged to act through state agency, depend on state opinion and
state discretion.... If anything be found in the national constitution,
either by original provisions, or subsequent interpretation, which
ought not to be in it, the people know how to get rid of it. If any
construction be established, unacceptable to them, so as to become
practically a part of the constitution, they will amend it at their own
sovereign pleasure. But while the people choose to maintain it as it
is—while they are satisfied with it, and refuse to change it—who has
given, or who can give, to the state legislatures a right to alter it,
either by interference, construction, or otherwise?... Sir, the
people have not trusted their safety, in regard to the general
constitution, to these hands. They have required other security, and
taken other bonds.” (From Webster’s reply to Hayne, U. S. Senate,
January, 1830. 4 Ell. Deb. 498 et seq.)
We average Americans, now educated in the experience of the
average American from 1776 to the beginning of 1787, find much
merit and comfort in Webster’s understanding of basic American law.
He had a reasoned and firm conviction that Americans really are
citizens and not subjects. His conviction, in that respect, while
opposed to the convictions of our leaders and “constitutional”
lawyers, has seemed to us quite in accord with the convictions of
earlier leaders such as Iredell and Wilson and the others, and also
with the decisions of our Supreme Court.
Briefly stated, it has become quite clear to us that the American
people, from 1776 to 1787, were fixed in their determination to make
our basic American law what the conviction of Webster and the
leaders of every generation prior to our own knew it to be. Let us go
back, therefore, to the Americans in the Philadelphia convention of
1787, who worded the Constitution which is the supreme law of
America, and ascertain how their knowledge of fundamental
American law dictated the wording of their proposed Seventh Article.
CHAPTER VIII
PHILADELPHIA ANSWERS “CONVENTIONS, NOT
LEGISLATURES”

We recall how clearly the Americans at Philadelphia, in 1787,


knew that any grant of national power to interfere with the freedom of
individuals was the constitution of government. We recall the bitter
conflict of opinion, threatening the destruction of the assembly, over
the manner of choosing the members of the legislature to exercise
whatever powers of that kind the citizens of America might grant. We
recall the great opposition to the proposal of a grant of any power of
that kind and to the particular proposal of each of the enumerated
powers of that kind, all embodied in the First Article.
We have thus come to know with certainty that the minds of the
Americans at Philadelphia, during those strenuous four months,
were concentrated mainly upon a proposal to grant some national
power to interfere with the human freedom of all Americans. In other
words, we have their knowledge that their proposed First Article, by
reason of its grants of such power, would constitute a new nation
and government of men, if those grants were validly made by those
competent to make such grants.
Under which circumstances, we realize that it became necessary
for them to make a great legal decision, in the construction of basic
American law, and, before making that decision, which was
compelled to be the result of judgment and not of will, accurately to
ascertain one important legal fact. Indeed, their decision was to be
the actual conclusion reached in the effort to ascertain that legal fact.
This was the single question to which they must find the right
answer: “Under our basic American law, can legislatures ever give to
government any power to interfere with the human freedom of men,
or must every government in America obtain its only valid powers of
that kind by direct grant from its own citizens?”
It is easy for us to state that they should have known that the
answer to that question was expressly and authoritatively given in
the Statute of ’76. It was there plainly enacted that every just power
of any government must be derived from the direct grant of those to
be governed by its exercise. Yet our own leaders for the last five
years have not even asked the question, much less known the right
answer.
At Philadelphia, in 1787, they did know it. They had no doubt
whatever about it. We shall see that quickly in our brief review of the
record they made at Philadelphia in ascertaining and deciding, as a
legal necessity, to whom their First Article and its enumerated grants
of national power must be sent and, when we boast of how quickly
we knew the answer, we should admit that we did not know it until
after we had lived again with them through their experience of the
preceding ten or twelve years which had educated them, as it has
just educated us, to that knowledge. Furthermore, many of us
average Americans will be unable to explain, until later herein, why,
during the last five years, our own leaders have not known the right
answer. The Statute of ’76 has not been wholly unknown to them.
The record of the Philadelphia Convention and the ratifying
conventions has not been entirely a closed book to them. The
important and authentic statements of Webster and other leaders of
past generations have been read by many of them. If they did not
understand and know the correct answer, as we now realize they
have not known, let us not withhold from the Americans at
Philadelphia our just tribute of gratitude that they did accurately
know, when it was amazingly important to us that they should know.
When those Americans came to answer that question, there were
facts which might have misled them as other similar facts of lesser
importance have undoubtedly misled our leaders.
In 1776, from that same Philadelphia had gone a suggestion that a
constitution of government, with Articles granting power to
government, be made in each former colony. In 1787, there had
gone from that same Philadelphia a proposal that a constitution of a
general government for America be made, with Articles granting
power to that government. The proposal of 1776 had suggested that
the proposed Articles be made by the people themselves, assembled
in conventions. The proposal of 1777 had suggested that the
proposed Articles be made by the legislative governments of the
states. Both proposals, even as to the makers of the respective
Articles, had been acted upon. All the Articles, although some had
been made by the people themselves and others by legislatures, had
been generally recognized as valid law. Some of the men at
Philadelphia in 1787 had been members of the proposing Second
Continental Congress, when the respective proposals of 1776 and
1777 had gone from Philadelphia. When, in 1787, they were called
upon to find and state, as their legal decision, the correct answer to
their important question, it was necessary for them to ascertain, as
between state “legislatures” and the people themselves, in
“conventions,” which could validly make the Articles which had been
worded and were about to be proposed. It would not, therefore, have
been beyond the pale of our own experience if the earlier proposals
had misled them and they had made the wrong answer to the
question which confronted them. Furthermore, as we have already
noted, although we can little realize the influence of such a fact upon
men seeking the correct legal answer to an important question, their
whole proposal was a new adventure for men on an uncharted sea
of self-government. Under all of which circumstances, let us again
pay them their deserved tribute that they went unerringly to the only
correct answer.
We know that the essence of that answer is expressed in the
Seventh Article proposed from Philadelphia. Only one answer was
possible to Americans of that generation. They had been “subjects”
and had become “citizens.” They knew the vital distinctions between
the two relations to government.
The Convention which framed the Constitution was, indeed,
elected by the state legislatures. But the instrument, when it
came from their hands, was a mere proposal, without
obligation, or pretensions to it. It was reported to the then
existing Congress of the United States, with the request that it
might “be submitted to a convention of delegates, chosen in
each state by the people thereof, under the recommendation
of its legislature, for their assent and ratification.” This mode
of proceeding was adopted; and by the Convention, by
Congress, and by the state legislatures, the instrument was
submitted to the people. They acted upon it in the only
manner in which they can act safely, effectively, and wisely,
on such a subject, by assembling in convention. It is true, they
assembled in their several states; and where else should they
have assembled? No political dreamer was ever wild enough
to think of breaking down the lines which separate the states,
and of compounding the American people into one common
mass. Of consequence, when they [the American people] act,
they act in their states. But the measures they adopt do not,
on that account, cease to be the measures of the people
themselves, or become the measures of the state
governments. From these conventions the Constitution [the
First Article grants of power to interfere with individual
freedom] derives its whole authority. The government
proceeds directly from the people; is “ordained and
established” in the name of the people.... It required not the
affirmance, and could not be negatived, by the state
governments.... To the formation of a league, such as was the
Confederation, the state sovereignties were certainly
competent.
But, when a general government of America was to be given any
national power to interfere with the individual freedom of its citizens,
as in the First Article of 1787 and in the Eighteenth Amendment of
1917,
acting directly on the people, the necessity of referring it to
the people, and of deriving its powers directly from them, was
felt and acknowledged by all. The government of the Union,
then, (whatever may be the influence of this fact on the case,)
is, emphatically, and truly, a government of the people. In form
and in substance it emanates from them. Its powers are
granted by them, and are to be exercised directly on them,
and for their benefit. (Marshall in the Supreme Court,
M’Culloch v. Maryland, 4 Wheat. 316.)
Marshall was one of the Americans who had been at Valley Forge
in 1778, and at other places whose sacrifices made it the basic law
of America that all power over American citizens must be derived by
direct grant from themselves. Later, he was prominent in the Virginia
convention where all Americans in Virginia knew and acted upon this
basic law. These facts qualified him to testify, from the Bench of the
Supreme Court, that all Americans then knew and acknowledged the
binding command of that basic law.
Under such circumstances, it was impossible that the Americans
at Philadelphia should not have known and obeyed that law in the
drafting of their proposed Seventh and Fifth Articles. Both of these
Articles, the Seventh wholly, and the Fifth partly, deal with the then
future grant of national power over the people and its only legal gift
by direct grant from the people themselves, assembled in their
“conventions.” Both Articles name the people of America, by the one
word “conventions.”
That Philadelphia should not have strayed from the legal road
clearly marked by the Statute of ’76 was certain when we recall how
large a part Madison played at Philadelphia, and particularly how he
personally worded and introduced, in the closing hours at
Philadelphia, what we know as its Fifth Article. As to his personal
knowledge of this basic law, we recall his letter of April, 1787, where
he said, “To give the new system its proper energy, it will be
desirable to have it ratified by the authority of the people, and not
merely by that of the legislatures.” And we recall his later words,
when urging Americans to adopt the Constitution with its Fifth and
Seventh Articles, he said of the Seventh, “This Article speaks for
itself. The express authority of the people alone could give due
validity to the Constitution,” to its grants of power over the people in
its First Article. (Fed. No. 43.)
That we may fix firmly in our own minds the knowledge which all
Americans then had, which our leaders never acquired or have
entirely forgotten, let us briefly review what the earlier Americans did
at Philadelphia in obedience to that knowledge of basic American
law.
On May 28, Randolph of Virginia “opened the main business” of
the Convention. He proposed fifteen resolutions embodying the
suggestion of what should be in the different Articles. Resolution
Number 15 was that such Articles should be submitted to
“conventions,” “to be expressly chosen by the people, to consider
and decide thereon.” (5 Ell. Deb. 128.)
The first short debate on this Resolution took place on June 5. In it
Madison stated that he “thought this provision essential. The Articles
of Confederation themselves were defective in this respect, resting,
in many of the states, on the legislative sanction only.” The resolution
was then postponed for further consideration. On June 12, “The
question was taken on the 15th Resolution, to wit, referring the new
system to the people of the United States for ratification. It passed in
the affirmative.” (5 Ell. Deb. 183.) This was all in the Committee of
the Whole.
On June 13, that Committee made their full report, in which the
Randolph Resolution Number 15 was embodied in words as
Resolution Number 19 of the report. On June 16, while the
Convention was again sitting as a Committee of the Whole, the great
struggle was on between the conflicting opinions as to how and in
what proportion should be elected the future legislators who were to
exercise the granted powers over Americans. On that day, the
discussion centered on the relative merits of the Randolph national
proposals and a set of federal Articles amending the existing Federal
Constitution. In supporting Randolph, Wilson of Pennsylvania stated
that “he did not fear that the people would not follow us into a
national government; and it will be a further recommendation of Mr.
Randolph’s plan that it is to be submitted to them, and not to the
legislatures, for ratification.” (5 Ell. Deb. 196.)
On July 23, Resolution Number 19 came up for action.
Remembering how insistent many of the delegates were that the
general government should be kept a purely federal one, it is not
surprising to find Oliver Ellsworth of Connecticut opening the short
debate with a motion that the Constitution “be referred to the
legislatures of the states for ratification.” But it will also be
remembered that the powers to be granted in the new Articles had
not yet been settled. The nationalists in the Convention, intent on
having some national Articles, knew that the proposed ratification
must be by the people themselves, “felt and acknowledged by all” to
be the only competent grantors of national powers.
Colonel Mason of Virginia “considered a reference of the plan to
the authority of the people as one of the most important and
essential of the resolutions. The legislatures have no power to ratify
it. They are the mere creatures of the state constitutions, and cannot
be greater than their creators.... Whither, then, must we resort? To
the people, with whom all power remains that has not been given up
in the constitutions derived from them. It was of great moment that
this doctrine should be cherished, as the basis of free government.”
(5 Ell. Deb. 352.)
Rufus King of Massachusetts, influenced undoubtedly by the error
of thinking that the Convention meant to act within the Articles of
Confederation, was inclined to agree with Ellsworth “that the
legislatures had a competent authority, the acquiescence of the
people of America in the Confederation being equivalent to a formal
ratification by the people.... At the same time, he preferred a
reference to the authority of the people, expressly delegated to
conventions, as the most certain means of obviating all disputes and
doubts concerning the legitimacy of the new Constitution.” (5 Ell.
Deb. 355.)
Madison “thought it clear that the legislatures were incompetent to
the proposed changes. These changes would make essential
inroads on the state constitutions; and it would be a novel and
dangerous doctrine, that a legislature could change the constitution
under which it held its existence.” (5 Ell. Deb. 355.)
Ellsworth’s motion to send to the state legislative governments,
and not to the people themselves, assembled in “conventions,” was
lost by a vote of seven to three. Resolution Number 19, that the new
Articles must be sent to the people themselves was adopted by a
vote of nine to one, Ellsworth and King both voting for it. (5 Ell. Deb.
356.)
This impressive discussion, now continued for over a month of
1787, with its display of accurate knowledge of the distinction
between sending Articles to legislatures and “referring” them to the
people, makes quite amusing what we shall hear later in 1917. It will
come from the counsel of the political organization which dictated
that governments should make the supposed Eighteenth
Amendment. After he kindly tells us that history has proven that
these Americans of 1787 “builded more wisely than they knew,”
meaning “than he knew,” he shall later impart to us the remarkable
information that “the framers in the Constitutional Convention knew
very little, if anything, about referendums.”
The Resolutions, which had now become twenty-three in number,
on July 26, were referred to the Committee of Detail to prepare
Articles in conformity therewith. On August 6, that Committee made
its report of twenty-three worded Articles. In Article XXII was
embodied the requirement that the Constitution should be submitted
“to a convention chosen in each state, under the recommendation of
its legislature, in order to receive the ratification of such convention.”
This provision, the Philadelphia answer and always the only legal
answer to the question as to who can validly grant power to interfere
with individual freedom, was later seen not properly to belong in the
Constitution itself. For which reason, it was taken out of the
Constitution and embodied in a separate Resolution which went with
the Constitution from Philadelphia.
In Article XXI, the first draft of our Article VII, it was provided: “The
ratification of the conventions of —— states shall be sufficient for
organizing this Constitution.” (5 Ell. Deb. 381.)
The month of August was passed in the great debates on the
proposed grants of national power and the other proposed Articles.
When the Convention was drawing to a close on August 30, Articles
XXI and XXII were reached.
Gouverneur Morris of Pennsylvania “moved to strike out of Article
XXI the words, ‘conventions of the,’ after ‘ratification,’ leaving the
states to pursue their own modes of ratification.” Rufus King “thought
that striking out ‘conventions,’ as the requisite mode, was equivalent
to giving up the business altogether.” Madison pointed out that, “The
people were, in fact, the fountain of all power.” The motion of Morris
was beaten. An attempt was made to fill the blank in Article XXI with
the word “thirteen.” “All the states were ‘No’ except Maryland.” The
blank was then filled by the word “nine” the vote being eight to three.
The two articles were then passed, the vote thereon being ten to
one. (5 Ell. Deb. 499-502.)
On September 10, the beginning of the last business week of the
Convention, Gerry of Massachusetts moved to reconsider these two
Articles. The short discussion was not in connection with any matter
in which we are now interested. His motion was lost. The entire set
of worded Articles was then referred to a committee for revising the
style and arrangement of the Articles agreed upon. (5 Ell. Deb. 535.)
On Wednesday, September 12, that Committee reported our
Constitution, with its seven Articles, as we know them except for
some slight changes made during the discussions of the last three or
four days of the Convention. In these seven Articles, the language of
the earlier Article XXII did not appear. As it really was the statement
of the correct legal conclusion of the Convention that its proposed
Articles, because they would grant power to interfere with individual
freedom, must necessarily be made by the people themselves, its
proper place was outside the Constitution itself and in a special
Resolution of the same nature as every Congress resolution
proposing an amendment to that Constitution. That was the view of
the Committee and, on Thursday, September 13, the Committee
reported such special Resolutions, in the very words of the former
Article XXII. “The proceedings on these Resolutions are not given by
Mr. Madison, nor in the Journal of the Federal Convention. In the
Journal of Congress, September 28, 1787, Volume 4, p. 781, they
are stated to have been presented to that body, as having passed in
the Convention on September 17 immediately after the signing of the
Constitution.” (5 Ell. Deb. 602.)
This is the Resolution:
“Resolved, That the preceding Constitution be laid before the
United States in Congress assembled; and that it is the opinion of
this Convention, that it should afterwards be submitted to a
convention of delegates chosen in each state by the people thereof,
under the recommendation of its legislature, for their assent and
ratification; and that each convention, assenting to and ratifying the
same, should give notice thereof to the United States in Congress
assembled.
“Resolved, That it is the opinion of this Convention, that, as soon
as the conventions of nine states shall have ratified this Constitution,
the United States in Congress assembled should fix a day, etc.” (5
Ell. Deb. 541.)
This Resolution is the most authoritative statement of the legal
conclusion reached by these leaders of a people then “better
acquainted with the science of government than any other people in
the world.” The conclusion itself was compelled by accurate
knowledge that the government of “citizens” can validly obtain only
from the citizens themselves, by their direct grant, any power to
interfere with their individual freedom. The expression of that
knowledge, in the Resolution, is, in many respects, one of the most
important recorded legal decisions ever made in America. We
average Americans, educated with those Americans at Philadelphia
through their experience of the years between 1775 and 1787,
cannot misunderstand the meaning and importance of that decision.
Instructed by our review of their actions and their reasoning at
Philadelphia in reaching that conclusion and making that legal
decision, we know, with an accurate certainty, that it was their
declaration to the world and to us that no proposal from Philadelphia
suggested that Americans again resume the relation of “subjects” to
any government or governments.
Our minds impressed with this accurate knowledge that such was
not their purpose, we now prepare to complete our education as
American citizens, not subjects, by reading the Philadelphia story
and language of their Fifth Article, their only other Article which even
partly concerned the future grant of new government power to
interfere with individual American freedom. By reason of our
education, we will then come to the reading of the language of this
Article, as the Americans read it and understood it when they made it
in their “conventions” that followed the proposing convention of
Philadelphia.
Being educated “citizens” and not “subjects,” we ourselves will no
longer, as our leaders have done for five years, mistake the only
correct and legal answer to the indignant outburst of Madison, who
wrote this Fifth Article at Philadelphia. “Was, then, the American
Revolution effected, was the American Confederacy formed, was the
precious blood of thousands spilt, and the hard-earned substance of
millions lavished, not that the people of America should enjoy peace,
liberty, and safety, but that the governments of the individual states,
that particular municipal establishments, might enjoy a certain extent
of power, and be arrayed with certain dignities and attributes of
sovereignty? We have heard of the impious doctrine in the Old
World, that the people were made for kings, not kings for the people.
Is the same doctrine to be revived in the New, in another shape—
that the solid happiness of the people is to be sacrificed to the views
of political institutions of a different form?” (Fed. No. 45.)
The American answer, from the people of America assembled in
the conventions that ratified that Fifth Article, was a clear and
emphatic “No.” The Tory answer of the last five years, from our
leaders and our governments, has been an insistent “Yes.”
No one, however, with any considerable degree of truthfulness,
can assert that there has come from the American people
themselves, during the last five years, any very audible “Yes.” To
whatever extent individual opinions may differ as to the wisdom or
legality of the new constitution of government of men, made entirely
by governments, no unbiased observer has failed to note one
striking fact. By a very extensive number of Americans otherwise
law-abiding, Americans in all classes of society, the new government
edict, the government command to “subjects,” has been greeted with
a respect and obedience strikingly similar to the respect and
obedience with which an earlier generation of Americans received
the Stamp Act and the other government edicts between 1765 and
1776.
When the Americans of that earlier generation were denounced by
the government which had issued those edicts to its “subjects,” one
of the latter, five years before Americans ceased to be “subjects” of
that government, stated: “Is it a time for us to sleep when our free
government is essentially changed, and a new one is forming upon a
quite different system—a government without the least dependence
upon the people?”
It may be but a coincidence that, while our American government
was announcing its recognition of the wide-spread American
disrespect for the new government edict, it is only a few days since
throughout America there resounded many eulogies of the Samuel
Adams, who made that statement in the Boston Gazette of October
7, 1771. In those eulogies, there was paid to him the tribute that he
largely helped to bring about the amazing result of American desire
for individual freedom which culminated in the assembling of the
Americans in the “conventions” which ratified the proposed
Constitution.
We have already sensed that the existence of the supposed
Eighteenth Amendment depends entirely upon an amazing modern
meaning put into the Fifth Article made in those conventions. Let us,
therefore, who are Americans now educated in the experience of the
Americans who assembled in those “conventions,” sit therein with
them and there read the story and the language of the Fifth Article as
they read it when they made it.
CHAPTER IX
THE FIFTH ARTICLE NAMES ONLY
“CONVENTIONS”

It has been the misfortune of our prominent Americans of this


generation that they read the Fifth Article with preconceived notions
of its meaning. To the error of that method of reading it, we average
Americans will not pay the tribute of imitation. We know that its
meaning to those who made it in the “conventions” of the earlier
century is the meaning which it must have as part of the supreme
law of the land. That we may read it as they read it and get its clear
and only possible meaning, as they got it, we shall briefly review the
story of its wording and its proposal at Philadelphia. That Convention
immediately preceded the assembling of the people in their own
“conventions.” In each of their “conventions,” among the people
assembled, were some who had been prominent at Philadelphia,
such as Madison and Randolph and Mason in Virginia, Hamilton in
New York, Wilson in Pennsylvania and the Pinckneys in South
Carolina. Moreover, between the Philadelphia proposal and the
assembling of these conventions, Madison and Hamilton, proposer
and seconder of the Fifth Article at Philadelphia, had been publishing
their famous essays, now collectively known as The Federalist, in
the New York newspapers to explain the Articles worded at
Philadelphia and to urge their adoption. Under which circumstances,
it is clear that, if we want to read and know the meaning of the Fifth
Article as it was understood in those conventions, the Fifth Article
which named those same “conventions,” we must complete our
education by an accurate and brief review of the story of that Article
at Philadelphia. Only in that way shall we average Americans of
today be in the position in which were the Americans who made that
Article.
When we read that story of Philadelphia, in relation to the Fifth
Article, one thing stands out with amazing clarity and importance.
We already know how that Convention, until its last days, was
concentrated upon the hotly debated question of its own proposed
grants of national powers in the First Article. In the light of which
continued concentration, it is not surprising to learn that, until almost
the very last days, the delegates forgot entirely to mention, in their
tentative Fifth Article, the existing and limited ability of state
legislatures to make federal or declaratory Articles, and mentioned
only “conventions” of the people, who alone could or can make
national Articles.
The first suggestion of what we now know as the Fifth Article was
on the second day, May 29, when the Randolph Resolution 13 read
“that provision ought to be made for the amendment of the Articles of
union whensoever it shall seem necessary.” This wording was the
exact language of Resolution 17 of the report of the Committee of
the Whole. It was adopted by the Convention on July 23. Three days
later, with the other Resolutions, it was referred to the Committee of
Detail “to prepare and report the Constitution.” On August 6, this
Committee, in the first draft of our Constitution, reported the
following: “Art. XIX. On the Application of the legislatures of two-
thirds of the states in the Union, for an amendment of this
Constitution, the legislature of the United States shall call a
convention for that purpose.”
We see clearly why the delegates, their minds concentrated on
their own proposed grants of national powers, mentioned only the
people themselves, the “conventions” of the “Seventh” and “Fifth”
Articles, who alone can make national Articles, and forgot to mention
legislatures, because the latter never can make national Articles.
That kind of Article was the only thing they were then thinking about.
Naturally, it then escaped their attention that, if they proposed a wise
and proper distribution of national power between the new American
government and the respective existing state governments, almost
every future Article, if not every one, would be of the federal kind,
which legislatures or governments could validly make, as they had
made all the Articles of the existing federation. Clearly for that
reason this Article XIX never even mentioned the existing and limited
ability of legislatures.
Between this report of August 6 and August 30, the Convention
was again entirely occupied with the grants of national power and
the election of the legislators to exercise it or, in other words, with
what is now the First Article. On August 30, Article XIX was adopted
without any debate.
We are now aware that the Convention was within two weeks of its
end and no one had mentioned, in what is now the Fifth Article, the
state governments or legislatures as possible makers of federal
Articles, if and when such Articles were to be made in the future.
It was not until September 10, Monday of the last Convention
week, that Article XIX again came up for action, when Gerry of
Massachusetts moved to reconsider it. His purpose, as he himself
stated it, was to object because it made it possible that, if the people
in two-thirds of the states called a convention, a majority of the
American people assembled in that convention “can bind the Union
with innovations that may subordinate the state constitutions
altogether.” Hamilton stated that he could see “no greater evil, in
subjecting the people in America to the major voice than the people
of any particular state.” He went on to say that he did think the Article
should be changed so as to provide a more desirable “mode for
introducing amendments,” namely, drafting and proposing them to
those who could make them. In this respect he said: “The mode
proposed was not adequate. The state legislatures will not apply for
alterations, but with a view to increase their own powers. The
national legislature will be the first to perceive, and will be most
sensible to, the necessity of amendments; and ought also to be
empowered, whenever two-thirds of each branch should concur, to
call a convention. There could be no danger in giving this power, as
the people would finally decide in the case.” (5 Ell. Deb. 531.)
Roger Sherman of Connecticut then tried to have the Article
provide that the national government might also propose
amendments to the several states, as such; such amendment to be
binding if consented to by the several states, namely, all the states.
For reasons that will appear in a moment, this clear attempt to
enable the states, mere political entities, and their legislatures,
always governments, to do what they might wish with the individual
freedom of the American citizen—thus making him their subject—
was never voted upon. It was, however, seconded by Gerry of
Massachusetts. Its probable appeal to Sherman, always a strong
opponent of the national government of individuals instead of the
federal government of states, was that it would make it difficult to
take away any power from Connecticut, unless Connecticut wished
to give it up. Its appeal to Gerry, consistently a Tory in his mental
attitude to the relation of government and human being, was
undoubtedly the fact that it would permit government or governments
to do what they might wish with individual freedom. It does not
escape the attention of the average American that our governments
and leaders, during the last five years, have not only displayed the
mental attitude of Gerry but have also acted as if the proposal, which
he urged, had been put into what is our Fifth Article. Only on that
theory can we average Americans, with our education, understand
why governments in America have undertaken to exercise and to
vest in our government a national power over us, which power
neither is enumerated in the First Article nor was ever granted by the
citizens of America to their only government; nor can we understand
why our leaders have assumed that governments in America, which
are not even the government of the American citizens, can do either
or both of these things. We know, if governments and leaders do not,
that neither thing can ever be possible in a land where men are
“citizens” and not “subjects.”
CHAPTER X
ABILITY OF LEGISLATURES REMEMBERED

Living through the days of that Convention, we have now seen


three months and ten days of its sincere and able effort to word a
Constitution which would “secure the Blessings of Liberty” to the
individual American. We have seen them spend most of their time in
the patriotic endeavor to adjust and settle how much, if any, national
power to interfere with individual freedom that Constitution shall give
to its only donee, the new and general government. In other words,
we have seen the mind and thought and will of that Convention
almost entirely concentrated, for those three months and ten days,
upon the Article which is the constitution of government, the First
Article, with its enumerated grants of general power to interfere with
the human rights of the American citizen.
Keeping in mind the object of that intense concentration, the First
Article grants of power of that kind, we average Americans note, with
determined intent never to forget, the effect of that concentration
upon the wording of our Fifth Article up to that tenth day of
September. We note, with determined intent never to forget, that,
from May 30 to September 10, the only maker of future changes
mentioned was the “people” of America, the most important reservee
of the Tenth Amendment, the “conventions” of the American people
named in both the Seventh and the Fifth Articles.
As this fact and its tremendous meaning have never been known
or mentioned in the sorry tale of the five years from 1917 to 1922, we
average Americans are determined to dwell upon it briefly so that we
cannot escape an accurate appreciation of the short remaining story
of the one week at Philadelphia, in 1787, in relation to our Fifth
Article.

You might also like