100% found this document useful (1 vote)
70 views39 pages

Immediate download C Basics Cheat Sheet Www.Begincodingnow.Com ebooks 2024

Sheet

Uploaded by

zutaklenam18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
70 views39 pages

Immediate download C Basics Cheat Sheet Www.Begincodingnow.Com ebooks 2024

Sheet

Uploaded by

zutaklenam18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Experience Seamless Full Ebook Downloads for Every Genre at textbookfull.

com

C Basics Cheat Sheet Www.Begincodingnow.Com

https://textbookfull.com/product/c-basics-cheat-sheet-www-
begincodingnow-com/

OR CLICK BUTTON

DOWNLOAD NOW

Explore and download more ebook at https://textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Commander in Cheat How Golf Explains Trump Rick Reilly

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

textboxfull.com

Japan Style Sheet Swet (Society Of Writers

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

textboxfull.com

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/
textboxfull.com

Tribology in Sheet Rolling Technology 1st Edition Akira


Azushima (Auth.)

https://textbookfull.com/product/tribology-in-sheet-rolling-
technology-1st-edition-akira-azushima-auth/

textboxfull.com
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/

textboxfull.com

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/

textboxfull.com

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/

textboxfull.com

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/

textboxfull.com

Ethics the Basics Mizzoni

https://textbookfull.com/product/ethics-the-basics-mizzoni/

textboxfull.com
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 Scribd Document
with Unrelated Content
A Kairouan, que je ne connaissais pas encore, et où j’allai sans
Marceline, la nuit était très belle. Au moment de rentrer dormir à
l’hôtel, je me souvins d’un groupe d’Arabes couchés en plein air sur
les nattes d’un petit café. Je m’en fus dormir tout contre eux. Je
revins couvert de vermine.

La chaleur moite de la côte affaiblissant beaucoup Marceline, je


lui persuadai que ce qu’il nous fallait, c’était gagner Biskra au plus
vite. Nous étions au début d’avril.
Ce voyage est très long. Le premier jour, nous gagnons d’une
traite Constantine ; le second jour, Marceline est très lasse et nous
n’allons que jusqu’à El Kantara. Là nous avons cherché et nous
avons trouvé vers le soir une ombre plus délicieuse et plus fraîche
que la clarté de la lune, la nuit. Elle était comme un breuvage
intarissable ; elle ruisselait jusqu’à nous. Et du talus où nous étions
assis, on voyait la plaine embrasée. Cette nuit, Marceline ne peut
dormir ; l’étrangeté du silence et des moindres bruits l’inquiète. Je
crains qu’elle n’ait un peu de fièvre. Je l’entends se remuer sur son
lit. Le lendemain, je la trouve plus pâle. Nous repartons.
Biskra. C’est donc là que je veux en venir. Oui ; voici le jardin
public ; le banc… je reconnais le banc où je m’assis aux premiers
jours de ma convalescence. Qu’y lisais-je donc ?… Homère ; depuis je
ne l’ai pas rouvert. — Voici l’arbre dont j’allai palper l’écorce. Que
j’étais faible, alors !… Tiens ! voici des enfants… Non, je n’en
reconnais aucun. Que Marceline est grave ! Elle est aussi changée
que moi. Pourquoi tousse-t-elle, par ce beau temps ? — Voici l’hôtel.
Voici nos chambres ; nos terrasses. — Que pense Marceline ? Elle ne
m’a pas dit un mot. — Sitôt arrivée dans sa chambre, elle s’étend
sur le lit ; elle est lasse et dit vouloir dormir un peu. Je sors.
Je ne reconnais pas les enfants, mais les enfants me
reconnaissent. Prévenus de mon arrivée, tous accourent. Est-il
possible que ce soient eux ? Quelle déconvenue ! Que s’est-il donc
passé ? Ils ont affreusement grandi… En à peine un peu plus de
deux ans — cela n’est pas possible… quelles fatigues, quels vices,
quelles paresses, ont déjà mis tant de laideur sur ces visages, où
tant de jeunesse éclatait ? Quels travaux vils ont déjeté si tôt ces
beaux corps ? Il y a là comme une banqueroute… Je questionne.
Bachir est garçon plongeur d’un café ; Ashour gagne à grand’peine
quelques sous à casser les cailloux des routes ; Hammatar a perdu
un œil. Qui l’eût cru : Sadeck s’est rangé ; il aide un frère aîné à
vendre des pains au marché ; il semble devenu stupide. Agib s’est
établi boucher près de son père ; il engraisse ; il est laid ; il est riche ;
il ne veut plus parler à ses compagnons déclassés… Que les carrières
honorables abêtissent ! Vais-je donc retrouver chez eux ce que je
haïssais parmi nous ? — Boubaker ? — Il s’est marié. Il n’a pas
quinze ans. C’est grotesque. — Non, pourtant ; je l’ai revu le soir. Il
s’explique : son mariage n’est qu’une frime. C’est, je crois, un sacré
débauché ! Mais il boit ; se déforme… Et voilà donc tout ce qui reste ?
Voilà donc ce qu’en fait la vie ! — Je sens à mon intolérable tristesse
que c’était beaucoup eux que je venais revoir. — Ménalque avait
raison : le souvenir est une invention de malheur.
Et Moktir ? — Ah ! celui-là sort de prison. Il se cache. Les autres
ne fraient plus avec lui. Je voudrais le revoir. Il était le plus beau
d’eux tous ; va-t-il me décevoir aussi ?… On le retrouve. On me
l’amène. — Non ! celui-là n’a pas failli. Même mon souvenir ne me le
représentait pas si superbe. Sa force et sa beauté sont parfaites. En
me reconnaissant, il sourit.
— Et que faisais-tu donc avant d’être en prison ?
— Rien.
— Tu volais ?
Il proteste.
— Que fais-tu maintenant ?
Il sourit.
— Eh ! Moktir ! si tu n’as rien à faire, tu nous accompagneras à
Touggourt. — Et je suis pris soudain du désir d’aller à Touggourt.
Marceline ne va pas bien ; je ne sais pas ce qui se passe en elle.
Quand je rentre à l’hôtel ce soir-là, elle se presse contre moi sans
rien dire, les yeux fermés. Sa manche large, qui se relève, laisse voir
son bras amaigri. Je la caresse, et la berce longtemps, comme un
enfant que l’on veut endormir. Est-ce l’amour, ou l’angoisse, ou la
fièvre qui la fait trembler ainsi ?… Ah ! peut-être il serait temps
encore… Est-ce que je ne m’arrêterai pas ? — J’ai cherché, j’ai trouvé
ce qui fait ma valeur : une espèce d’entêtement dans le pire. — Mais
comment arrivé-je à dire à Marceline que demain nous partons pour
Touggourt ?…
A présent, elle dort dans la chambre voisine. La lune, depuis
longtemps levée, inonde à présent la terrasse. C’est une clarté
presque effrayante. On ne peut pas s’en cacher. Ma chambre a des
dalles blanches, et là surtout elle paraît. Son flot entre par la fenêtre
grande ouverte. Je reconnais sa clarté dans la chambre et l’ombre
qu’y dessine la porte. Il y a deux ans elle entrait plus avant encore…
oui, là précisément où elle avance maintenant — quand je me suis
levé renonçant à dormir. J’appuyais mon épaule contre le montant
de cette porte-là. Je reconnais l’immobilité des palmiers… Quelle
parole avais-je donc lue ce soir-là ?… Ah ! oui ; les mots du Christ à
Pierre : « Maintenant tu te ceins toi-même, et tu vas où tu veux
aller… » Où vais-je ? Où veux-je aller ?… Je ne vous ai pas dit que, de
Naples, cette dernière fois, j’avais gagné Pœstum, un jour, seul…
Ah ! j’aurais sangloté devant ces pierres ! L’ancienne beauté
paraissait, simple, parfaite, souriante — abandonnée. L’art s’en va de
moi, je le sens. C’est pour faire place à quoi d’autre ? Ce n’est plus,
comme avant, une souriante harmonie… Je ne sais plus, à présent,
le dieu ténébreux que je sers. O Dieu neuf ! donnez-moi de connaître
encore des races nouvelles, des types imprévus de beauté.
Le lendemain, dès l’aube, la diligence nous emmène. Moktir est
avec nous. Moktir est heureux comme un roi.

Chegga ; Kefeldorh’; M’reyer… mornes étapes sur la route plus


morne encore, interminable. J’aurais cru pourtant, je l’avoue, plus
riantes ces oasis. Mais plus rien que la pierre et le sable ; puis
quelques buissons nains, bizarrement fleuris ; parfois quelque essai
de palmiers qu’alimente une source cachée… A l’oasis je préfère à
présent le désert — ce pays de mortelle gloire et d’intolérable
splendeur. L’effort de l’homme y paraît laid et misérable. Maintenant
toute autre terre m’ennuie.
— Vous aimez l’inhumain, dit Marceline. Mais comme elle regarde
elle-même ! et avec quelle avidité !
Le temps se gâte un peu, le second jour ; c’est-à-dire que le vent
s’élève et que l’horizon se ternit. Marceline souffre ; le sable qu’on
respire brûle, irrite sa gorge : la surabondante lumière fatigue son
regard ; ce paysage hostile la meurtrit. Mais à présent il est trop tard
pour revenir. Dans quelques heures, nous serons à Touggourt.
C’est de cette dernière partie du voyage, pourtant si proche
encore, que je me souviens le moins bien. Impossible, à présent, de
revoir les paysages du second jour et ce que je fis d’abord à
Touggourt. Mais ce dont je me souviens encore, c’est quelles étaient
mon impatience et ma précipitation.
Il avait fait très froid le matin. Vers le soir, un simoun ardent
s’élève. Marceline, exténuée par le voyage, s’est couchée sitôt
arrivée. J’espérais trouver un hôtel un peu plus confortable ; notre
chambre est affreuse ; le sable, le soleil et les mouches ont tout
terni, tout sali, défraîchi. N’ayant presque rien mangé depuis
l’aurore, je fais servir aussitôt le repas ; mais tout paraît mauvais à
Marceline et je ne peux la décider à rien prendre. Nous avons
emporté de quoi faire du thé. Je m’occupe à ces soins dérisoires.
Nous nous contentons, pour dîner, de quelques gâteaux secs et de
ce thé, auquel l’eau salée du pays a donné son goût détestable.
Par un dernier semblant de vertu, je reste jusqu’au soir auprès
d’elle. Et soudain je me sens comme à bout de forces moi-même. O
goût de cendres ! O lassitude ! Tristesse du surhumain effort ! J’ose à
peine la regarder ; je sais trop que mes yeux, au lieu de chercher
son regard, iront affreusement se fixer sur les trous noirs de ses
narines ; l’expression de son visage souffrant est atroce. Elle non
plus ne me regarde pas. Je sens, comme si je la touchais, son
angoisse. Elle tousse beaucoup ; puis s’endort. Par moments, un
frisson brusque la secoue.
La nuit pourrait être mauvaise et, avant qu’il ne soit trop tard, je
veux savoir à qui je pourrais m’adresser. Je sors. Devant la porte de
l’hôtel, la place de Touggourt, les rues, l’atmosphère même est
étrange au point de me faire croire que ce n’est pas moi qui les vois.
Après quelques instants je rentre. Marceline dort tranquillement. Je
m’effrayais à tort ; sur cette terre bizarre, on suppose un péril
partout ; c’est absurde. Et, suffisamment rassuré, je ressors.
Étrange animation nocturne sur la place ; circulation silencieuse ;
glissement clandestin des burnous blancs. Le vent déchire par
instants des lambeaux de musique étrange et les apporte je ne sais
d’où. Quelqu’un vient à moi… C’est Moktir. Il m’attendait, dit-il, et
pensait bien que je ressortirais. Il rit. Il connaît bien Touggourt, y
vient souvent et sait où il m’emmène. Je me laisse entraîner par lui.
Nous marchons dans la nuit ; nous entrons dans un café maure ;
c’est de là que venait la musique. Des femmes arabes y dansent —
si l’on peut appeler une danse ce monotone glissement. — Une
d’elles me prend par la main ; je la suis ; c’est la maîtresse de
Moktir ; il accompagne. Nous entrons tous les trois dans l’étroite et
profonde chambre où l’unique meuble est un lit ; un lit très bas, sur
lequel on s’assied. Un lapin blanc, enfermé dans la chambre,
s’effarouche d’abord, puis s’apprivoise et vient manger dans la main
de Moktir. On nous apporte du café. Puis, tandis que Moktir joue
avec le lapin, cette femme m’attire à elle, et je me laisse aller à elle
comme on s’abandonne au sommeil.
Ah ! je pourrais ici feindre ou me taire ; mais que m’importe à moi
ce récit, s’il cesse d’être véritable ?
Je retourne seul à l’hôtel, Moktir restant là-bas pour la nuit. Il est
tard. Il souffle un sirocco aride ; c’est un vent tout chargé de sable,
et torride malgré la nuit ; un vent de fièvre qui aveugle et fauche les
jarrets ; mais j’ai soudain trop hâte de rentrer, et c’est presque en
courant que je reviens. Elle s’est réveillée peut-être ; peut-être a-t-
elle besoin de moi ?… Non ; la croisée de la chambre est sombre ;
elle dort. J’attends un court répit du vent pour ouvrir ; j’entre très
doucement dans le noir. — Quel est ce bruit ?… Je ne reconnais pas
sa toux… Est-ce bien elle ?… J’allume…
Marceline est assise à moitié sur son lit ; un de ses maigres bras
se cramponne aux barreaux du lit, la tient dressée ; ses draps, ses
mains, sa chemise, sont inondés d’un flot de sang ; son visage en est
tout sali ; ses yeux sont hideusement agrandis ; et n’importe quel cri
d’agonie m’épouvanterait moins que son silence. Je cherche sur son
visage transpirant une petite place où poser un affreux baiser ; le
goût de sa sueur me reste aux lèvres. Je lave et rafraîchis son front,
ses joues. Contre le lit, quelque chose de dur sous mon pied : je me
baisse, et ramasse le petit chapelet qu’elle réclamait naguère à Paris,
et qu’elle a laissé tomber ; je le passe à sa main ouverte, mais sa
main aussitôt s’abaisse et le laisse tomber de nouveau. Je ne sais
que faire ; je voudrais demander du secours… Sa main s’accroche à
moi désespérément, me retient ; ah ! croit-elle donc que je veux la
quitter ? Elle me dit :
— Oh ! tu peux bien attendre encore. Elle voit que je veux
parler :
— Ne me dis rien, ajoute-t-elle ; tout va bien. — De nouveau je
ramasse le chapelet ; je le lui remets dans la main, mais de nouveau
elle le laisse — que dis-je ? elle le fait tomber. Je m’agenouille auprès
d’elle et presse sa main contre moi.
Elle se laisse aller, moitié contre le traversin et moitié contre mon
épaule, semble dormir un peu, mais ses yeux restent grands
ouverts.
Une heure après, elle se redresse ; sa main se dégage des
miennes, se crispe à sa chemise et en déchire la dentelle. Elle
étouffe. — Vers le petit matin, un nouveau vomissement de sang…

J’ai fini de vous raconter mon histoire. Qu’ajouterais-je de plus ?


— Le cimetière français de Touggourt est hideux, à moitié dévoré
par les sables… Le peu de volonté qui me restait, je l’ai tout employé
à l’arracher de ces lieux de détresse. C’est à El Kantara qu’elle
repose, dans l’ombre d’un jardin privé qu’elle aimait. Il y a de tout
cela trois mois à peine. Ces trois mois ont éloigné cela de dix ans.
Michel resta longtemps silencieux. Nous nous taisions aussi, pris
chacun d’un étrange malaise. Il nous semblait, hélas ! qu’à nous la
raconter, Michel avait rendu son action plus légitime. De ne savoir où
la désapprouver, dans la lente explication qu’il en donna, nous en
faisait presque complices. Nous y étions comme engagés. Il avait
achevé ce récit sans un tremblement dans la voix, sans qu’une
inflexion ni qu’un geste témoignât qu’une émotion quelconque le
troublât, soit qu’il mît un cynique orgueil à ne pas nous paraître
ému, soit qu’il craignît, par une sorte de pudeur, de provoquer notre
émotion par ses larmes, soit enfin qu’il ne fût pas ému. Je ne
distingue pas en lui, même à présent, la part d’orgueil, de force, de
sécheresse ou de pudeur. — Au bout d’un instant, il reprit :
Ce qui m’effraie, c’est, je l’avoue, que je suis encore très jeune. Il
me semble parfois que ma vraie vie n’a pas encore commencé.
Arrachez-moi d’ici à présent, et donnez-moi des raisons d’être. Moi,
je ne sais plus en trouver. Je me suis délivré, c’est possible ; mais
qu’importe ? je souffre de cette liberté sans emploi. Ce n’est pas,
croyez-moi, que je sois fatigué de mon crime, s’il vous plaît de
l’appeler ainsi ; mais je dois me prouver à moi-même que je n’ai pas
outrepassé mon droit.
J’avais, quand vous m’avez connu d’abord, une grande fixité de
pensée, et je sais que c’est là ce qui fait les vrais hommes ; je ne l’ai
plus. Mais ce climat, je crois, en est cause. Rien ne décourage autant
la pensée que cette persistance de l’azur. Ici toute recherche est
impossible, tant la volupté suit de près le désir. Entouré de splendeur
et de mort, je sens le bonheur trop présent et l’abandon à lui trop
uniforme. Je me couche au milieu du jour pour tromper la longueur
morne des journées et leur insupportable loisir. J’ai là, voyez, des
cailloux blancs que je laisse tremper à l’ombre, puis que je tiens
longtemps dans le creux de ma main, jusqu’à ce qu’en soit épuisée
la calmante fraîcheur acquise. Alors je recommence, alternant les
cailloux, remettant à tremper ceux dont la fraîcheur est tarie. Du
temps s’y passe, et vient le soir… Arrachez-moi d’ici ; je ne puis le
faire moi-même. Quelque chose en ma volonté s’est brisé ; je ne sais
même où j’ai trouvé la force de m’éloigner d’El Kantara. Parfois j’ai
peur que ce que j’ai supprimé ne se venge. Je voudrais
recommencer à neuf. Je voudrais me débarrasser de ce qui reste de
ma fortune ; voyez, ces murs en sont encore couverts. Ici je vis de
presque rien. Un aubergiste mi-français m’apprête un peu de
nourriture. L’enfant, que vous avez fait fuir en entrant, me l’apporte
soir et matin, en échange de quelques sous et de caresses. Cet
enfant qui, devant les étrangers, se fait sauvage, est avec moi
tendre et fidèle comme un chien. Sa sœur est une Ouled-Naïl qui,
chaque hiver, regagne Constantine où elle vend son corps aux
passants. Elle est très belle et je souffrais, les premières semaines,
que parfois elle passât la nuit près de moi. Mais, un matin, son frère,
le petit Ali, nous a surpris couchés ensemble. Il s’est montré fort
irrité et n’a pas voulu revenir de cinq jours. Pourtant il n’ignore pas
comment ni de quoi vit sa sœur ; il en parlait auparavant d’un ton
qui n’indiquait aucune gêne. Est-ce donc qu’il était jaloux ? — Du
reste, ce farceur en est arrivé à ses fins ; car, moitié par ennui,
moitié par peur de perdre Ali, depuis cette aventure je n’ai plus
retenu cette fille. Elle ne s’en est pas fâchée ; mais chaque fois que
je la rencontre, elle rit et plaisante de ce que je lui préfère l’enfant.
Elle prétend que c’est lui qui surtout me retient ici. Peut-être a-t-elle
un peu raison…
*** END OF THE PROJECT GUTENBERG EBOOK L'IMMORALISTE
***

Updated editions will replace the previous one—the old editions will
be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States copyright in
these works, so the Foundation (and you!) can copy and distribute it
in the United States without permission and without paying
copyright royalties. Special rules, set forth in the General Terms of
Use part of this license, apply to copying and distributing Project
Gutenberg™ electronic works to protect the PROJECT GUTENBERG™
concept and trademark. Project Gutenberg is a registered trademark,
and may not be used if you charge for an eBook, except by following
the terms of the trademark license, including paying royalties for use
of the Project Gutenberg trademark. If you do not charge anything
for copies of this eBook, complying with the trademark license is
very easy. You may use this eBook for nearly any purpose such as
creation of derivative works, reports, performances and research.
Project Gutenberg eBooks may be modified and printed and given
away—you may do practically ANYTHING in the United States with
eBooks not protected by U.S. copyright law. Redistribution is subject
to the trademark license, especially commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the free


distribution of electronic works, by using or distributing this work (or
any other work associated in any way with the phrase “Project
Gutenberg”), you agree to comply with all the terms of the Full
Project Gutenberg™ License available with this file or online at
www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand, agree
to and accept all the terms of this license and intellectual property
(trademark/copyright) agreement. If you do not agree to abide by all
the terms of this agreement, you must cease using and return or
destroy all copies of Project Gutenberg™ electronic works in your
possession. If you paid a fee for obtaining a copy of or access to a
Project Gutenberg™ electronic work and you do not agree to be
bound by the terms of this agreement, you may obtain a refund
from the person or entity to whom you paid the fee as set forth in
paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only be


used on or associated in any way with an electronic work by people
who agree to be bound by the terms of this agreement. There are a
few things that you can do with most Project Gutenberg™ electronic
works even without complying with the full terms of this agreement.
See paragraph 1.C below. There are a lot of things you can do with
Project Gutenberg™ electronic works if you follow the terms of this
agreement and help preserve free future access to Project
Gutenberg™ electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright law
in the United States and you are located in the United States, we do
not claim a right to prevent you from copying, distributing,
performing, displaying or creating derivative works based on the
work as long as all references to Project Gutenberg are removed. Of
course, we hope that you will support the Project Gutenberg™
mission of promoting free access to electronic works by freely
sharing Project Gutenberg™ works in compliance with the terms of
this agreement for keeping the Project Gutenberg™ name associated
with the work. You can easily comply with the terms of this
agreement by keeping this work in the same format with its attached
full Project Gutenberg™ License when you share it without charge
with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.

1.E. Unless you have removed all references to Project Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project Gutenberg™
work (any work on which the phrase “Project Gutenberg” appears,
or with which the phrase “Project Gutenberg” is associated) is
accessed, displayed, performed, viewed, copied or distributed:
This eBook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this eBook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning
of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the Project
Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt
that s/he does not agree to the terms of the full Project
Gutenberg™ License. You must require such a user to return or
destroy all copies of the works possessed in a physical medium
and discontinue all use of and all access to other copies of
Project Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
damaged disk or other medium, a computer virus, or computer
codes that damage or cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except for


the “Right of Replacement or Refund” described in paragraph 1.F.3,
the Project Gutenberg Literary Archive Foundation, the owner of the
Project Gutenberg™ trademark, and any other party distributing a
Project Gutenberg™ electronic work under this agreement, disclaim
all liability to you for damages, costs and expenses, including legal
fees. YOU AGREE THAT YOU HAVE NO REMEDIES FOR
NEGLIGENCE, STRICT LIABILITY, BREACH OF WARRANTY OR
BREACH OF CONTRACT EXCEPT THOSE PROVIDED IN PARAGRAPH
1.F.3. YOU AGREE THAT THE FOUNDATION, THE TRADEMARK
OWNER, AND ANY DISTRIBUTOR UNDER THIS AGREEMENT WILL
NOT BE LIABLE TO YOU FOR ACTUAL, DIRECT, INDIRECT,
CONSEQUENTIAL, PUNITIVE OR INCIDENTAL DAMAGES EVEN IF
YOU GIVE NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of receiving
it, you can receive a refund of the money (if any) you paid for it by
sending a written explanation to the person you received the work
from. If you received the work on a physical medium, you must
return the medium with your written explanation. The person or
entity that provided you with the defective work may elect to provide
a replacement copy in lieu of a refund. If you received the work
electronically, the person or entity providing it to you may choose to
give you a second opportunity to receive the work electronically in
lieu of a refund. If the second copy is also defective, you may
demand a refund in writing without further opportunities to fix the
problem.

1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the Foundation,


the trademark owner, any agent or employee of the Foundation,
anyone providing copies of Project Gutenberg™ electronic works in
accordance with this agreement, and any volunteers associated with
the production, promotion and distribution of Project Gutenberg™
electronic works, harmless from all liability, costs and expenses,
including legal fees, that arise directly or indirectly from any of the
following which you do or cause to occur: (a) distribution of this or
any Project Gutenberg™ work, (b) alteration, modification, or
additions or deletions to any Project Gutenberg™ work, and (c) any
Defect you cause.

Section 2. Information about the Mission


of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many
small donations ($1 to $5,000) are particularly important to
maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.
Project Gutenberg™ eBooks are often created from several printed
editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like