C# Fundamentals (1)
C# Fundamentals (1)
fundamenta
ls
• If you omit the using System line, you would have to write
System.Console.WriteLine() to print/output text.
Single-line comments start with two Multi-line comments start with /* and
forward slashes ( // ). ends with */.
Any text between // and the end of the Any text between /* and */ will be
line is ignored by C# (will not be ignored by C#. Meaningful eye contact
executed).
C#
Variables
Variables are containers for storing data
values. In C#, there are different types of
variables (defined with different keywords).
int - stores integers (whole numbers), without
decimals, such as 123 or -123
Examples of
variables in C#
Creating A
Variable
Creating A
Variable
Re-Assign
Value
Varieties of
types
Great Time
spent 😍
C#
Constants
If you don’t want others or yourself to
overwrite existing values, you can add the
const keyword in front of the variable type.
This will declare the variable as “constant”
which means unchangeable and read-only.
C#
Identifiers
All c# variables must be identified with
unique names. This unique name are
called identifiers.
It is recommended to use descriptive
name in other to create understandable
and maintainable code.
Data Type Size Description
A data type specifies
the size and type of Stores whole numbers from -
variable values. int 4 bytes 2,147,483,648 to
2,147,483,647
It is important to use Stores whole numbers from -
the correct data type long 8 bytes 9,223,372,036,854,775,808 to
for the 9,223,372,036,854,775,807
corresponding Stores fractional numbers.
variable; to avoid float 4 bytes Sufficient for storing 6 to 7
decimal digits
errors, to save time
and memory, but it Stores fractional numbers.
will also make your double 8 bytes Sufficient for storing 15 decimal
digits
code more
maintainable and
readable. bool 1 bytes Stores true or false values
Data Types In C#
Data Type Size Description
A data type specifies
the size and type of Stores a single
variable values. char 2 bytes character/letter, surrounded
by single quotes
It is important to use
the correct data type Stores a sequence of
for the string 8 bytes characters, surrounded by
corresponding double quotes
variable; to avoid
errors, to save time
and memory, but it
will also make your
code more
maintainable and
readable.
Data Types In C#
Numbers
Number types are divided into two Even though there are
groups: many numeric types in C#,
Integer types stores whole numbers, the most used for numbers
positive or negative (such as 123 or - are int (for whole numbers)
456), without decimals. Valid types are and double (for floating
int and long. Which type you should point numbers). However,
use, depends on the numeric value. we will describe them all
as you continue to read.
Floating point types represents
numbers with a fractional part,
containing one or more decimals. Valid
types are float and double.
Value Types & Reference Types
Value types directly contain their data. In C#, data types are
When you create a value type variable, the broadly categorized into
variable holds the actual value, not a two main types: value
reference to it. Value types are stored in the
stack memory (unless they are part of a
types and reference types.
reference type, in which case they are Understanding the
stored in the heap). difference between these
two is crucial for effective
Reference Types store a reference (or memory management and
memory address) to the actual data, which performance optimization
is stored in the heap memory. When you in C#.
create a reference type variable, the
variable holds a pointer to the memory
location where the data is stored.
Examples of Value Point Explanation
Types
Stored in Stack Value types are typically stored in the stack,
Primitive Types - int, Memory which makes them faster to access.
float, double, char, When you assign a value type to another
bool, etc. variable or pass it to a method, a copy of the
Copy by Value value is created. Changes to the copy do not
Structs – Custom affect the original.
structures defined using Value types cannot be null (unless they are
the struct keyword. Default Values nullable, e.g., int?). They have default values
(e.g., 0 for int, false for bool).
Enums - User-defined Value types have a fixed size determined at
enumerations. Fixed Size compile time.
Value Types In C#
Value Types
Code Sample:
Examples of Point Explanation
Reference Types
Stored in Heap Reference types are stored in the heap, which
Classes - Custom Memory allows for dynamic memory allocation.
classes defined using the
class keyword. When you assign a reference type to another
variable or pass it to a method, both variables
Arrays – Single- Copy by Reference point to the same memory location. Changes
dimensional or multi-
to one variable affect the other.
dimensional arrays.
Strings - The string Default Values The default value of a reference type is null.
type is a reference type.
Delegates – Function Reference types can grow or shrink in size
Dynamic Size dynamically (e.g., arrays, strings).
pointer
Interfaces - Reference
types that define
contracts.
Reference Types In C#
Reference Types
Code Sample:
Boxing & Unboxing
Type casting is when you assign a value of one data type to another type.
It is also possible to convert data types explicitly by using built-in methods, such as
Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int) and
Convert.ToInt64 (long):
Why Conversion?
Many times, there's no need for type conversion. But sometimes you
have to when working with user input, We would see an example in
the next chapter.
C# Operators
== Equal to x==y
!= Not equal x != y
String Length:
A string in C# is actually an object, which contain properties and methods that can
perform certain operations on strings. For example, the length of a string can be
found with the Length property:
C# String Concatenation
String Concatenation
The + operator can be used between strings to combine them. This is called
concatenation
C# String Interpolation
String Interpolation
Another option of string concatenation, is string interpolation, which substitutes
values of variables into placeholders in a string. Note that you do not have to worry
about spaces, like with concatenation:
C# If ... Else
The if Statement
C# While Loop
The while loop loops
through a block of code
as long as a specified
condition is True:
The Do/While Loop
Example Explained
static means that the method belongs to the Program class and not an
object of the Program class. You will learn more about objects and how
to access methods through objects later in this tutorial.
void means that this method does not have a return value. You will
learn more about return values later in this chapter
Call a Method