Table Of ContentLearn C# Quickly
A Complete Beginner’s Guide to
Learning C#, Even If You’re New to
Programming
CodeQuickly.org
ISBN: 978-1-951791-37-7
Copyright © 2020 by Code Quickly
ALL RIGHTS RESERVED
No part of this book may be reproduced, stored
in a retrieval system, or transmitted in any form
or by any means, electronic, mechanical,
photocopying, recording, scanning, or
otherwise, without the prior written permission
of the publisher.
Free Bonus + Source Code
Programming can be hard if you don't avoid these 7
biggest mistakes! Click below to get this free PDF
guide, and gain access to the source code for all of our
books.
codequickly.org/bonus
Contents
Chapter 1: Introductory Topics
1.1 - Data Types
1.2 - Integer
1.3 Decimal
1.4 - String
1.5 - Boolean
1.6 - Var
Chapter 2: Branches and Loops
2.1 - For Loop
2.2 - While Loop
2.3 - Recursion
2.4 - If…Else
Chapter 3: Methods and Properties
3.1 - Access modifiers
3.2 - Return values
Chapter 4: Classes and Objects
4.1 - Class
4.2 - Object
4.3 - Interface
Chapter 5: Collections
5.1 - List
5.2 - Dictionary
5.3 - ForEach
Chapter 6: Object-Oriented Programming
6.1. - Encapsulation
6.2 - Inheritance
6.3 - Abstraction
6.4 - Polymorphism
Chapter 7: SOLID Principles
7.1 - Single Responsibility Principle
7.2 - Open-Closed Principle
7.3 - Liskov
7.4 - Interface Segregation Principle
7.5 - Dependency Inversion Principle
Chapter 8: Advanced Topics
8.1. - Asynchronous Programming
8.2 - Parallel Programming
8.3 - LINQ
8.4 - Generics
8.5 - Dependency Injection
8.6 - Object Relational Mappers
8.7 - Mappers
8.8 - Unit Testing
Chapter 9: The Final Project
Chapter 10: Conclusion
Chapter 1: Introductory Topics
C# is an object-oriented programming language that has enormous
community support since it was built by Microsoft. C# is used for various
software development such as Web Applications, Desktop Applications,
Mobile Applications, etc.
1.1 - Data Types
Every variable in the program must belong to some data type, so if we need
to do some calculations, we will have to use numbers, and if we need to do
text manipulation, then we use characters, etc. Every data type has its own
usage circle and size of bytes inside of the memory when declared. Hence, it
is important to use the best data type for the related problem in order to avoid
errors and to make your code more readable and maintainable.
1.2 - Integer
An integer is one of the most used data types in C# language. The integer
represents any number that does not have fractional parts, also known as the
whole number (e.g., 1, 5, 27, 13, -145…). Integer has multiple types inside of
it, but the one we are mostly using is the Int32 data type. In the C# program,
the Int32 data type is commonly written as int. It is 32 bits in-memory sized.
The int variable can be declared, or it can be declared and value-assigned in
the same line. The default value for an integer data type is zero (0).
The most common integer types that you will probably see throughout the
code are:
1) byte – unsigned integer which is 8 bits represented in memory
2) long – it is 64 bits in-memory integer type (Int64)
3) short – integer type which is 16 bits organized in memory (Int16)
1.3 Decimal
Decimal data types in C# represent non-rounded numbers – fractional
numbers. There are also few Decimal data types that differ by its size in
memory, representation, usage, etc. In order to initialize the decimal data
type, a decimal keyword is used. This type is represented with 128 bit in
memory allocation. For decimal data types, the suffix -m, or –M must be
added at the end of the number so that the compiler knows that it should be
treated as a decimal type. If that suffix is not inserted, the compiler will treat
that number as the double type. Double type is the default floating-point
type in C#. That means if none of the suffixes is added at the end of the
number, that number will be treated as the double data type. It is represented
with 64 bits in memory allocation. There is also a float data type, whose size
in memory is 32 bits. Suffix for float data type representation is -f, or -F (if
not added, then the number will be treated as the double data type because
double is the default data type as mentioned earlier).
1.4 - String
String data type in C# is a type that represents text and works with text
variables. In fact, it is an array of characters. Declaring a string data type is
done by string keyword. When a string keyword is being used, it means that
it is referring to the 'System.String' class (classes will be explained later in the
book). There are many extension methods which could be used over some
string variable. Some of the most popular are Concat - which is
concatenating two strings, Contains - which determines whether the string
contains the given string from the parameter as passed value, Equals - which
determines whether two strings have the same value, etc. The default string
data type is an empty string. An example can be seen below.
1.5 - Boolean
The boolean data type is regarded as the logical data type in C#. It has only
two possible values, and they are true or false. The boolean type in code is
referred by the bool keyword. Boolean data types are used very often in
programming because it is always required to determine some logical
conditions. It is commonly used for comparing things such as number
comparison, object comparison, etc. Boolean is the returned value in every if
condition in the code (If-Else statement will be explained later in the
upcoming chapter). The default Boolean data type value is false.
1.6 - Var
Var data type is introduced in C# to implicitly declare variables. So, it can be
said that var is not a data type; it just represents a way of declaring variables.
What that means is that when some variables with the var keyword are
declared, we are telling the program that it needs to determine the explicit
data type of variable during the time of compilation. There are also
restrictions about the var keyword and its usage such that all explicit data
types such as int , string , and bool can be class fields.
On the other hand, implicit declaring (var keyword) cannot be class fields.
This “imaginary” data type can only be used if it is declared and initialized at
the same time (value-assigned) because the compiler will adjust its real data
type by the value from the right side of the equation mark. Variable declared
with the var keyword cannot be initialized with the null value.
Declaration and value initialization of the someVariable and
secondVariable is fine because the compiler knows how to determine the
data type for these two variables due to the right side of the assignment
(“New text” is the string data type and 53 is the integer data type). The
thirdVariable declaring is not allowed because it does not have any
initialization of the value. The fourthVariable initialization is also not
allowed because the compiler does not recognize the type by its right side
value assignment. It is null, and null stands for referencing the variable
pointer to nothing.
Let us jump to the examples.