JavaScript required
We’re sorry, but Coda doesn’t work properly without JavaScript enabled.
Skip to content
Gallery
Study Notes(Edit)
Resources
Networks
CAB201
Binary Calculator
Color Palette Finder
Database Management
More
Share
Explore
Exam Notes
Week 1 C# Fundamentals and Basics
1.
C# Data Types and Default Values
Value Types:
These hold data directly, and each type has a default value if not explicitly set.
Integer Types:
int
(32-bit):
Range:
-2,147,483,648 to 2,147,483,647,
Default Value:
0
long
(64-bit):
Range:
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807,
Default Value:
0L
byte
(8-bit):
Range:
0 to 255,
Default Value:
0
sbyte
(8-bit signed):
Range:
-128 to 127,
Default Value:
0
Unsigned Types:
Prefix
u
indicates unsigned types (e.g.,
uint
,
ulong
).
Floating Point Types:
float
(32-bit):
Precision:
~6-9 digits,
Default Value:
0.0f
double
(64-bit):
Precision:
~15-17 digits,
Default Value:
0.0d
decimal
(128-bit):
Precision:
~28-29 significant digits, used for high precision calculations,
Default Value:
0.0m
Character and Boolean Types:
char
: Represents a single Unicode character,
Default Value:
'\u0000'
bool
: Represents
true
or
false
,
Default Value:
false
Reference Types:
Store a reference to the memory location where data is held.
string
: Holds sequences of characters,
Default Value:
null
Nullability:
Reference types default to
null
if uninitialized, meaning they have no value until assigned.
2.
Variable Naming and Identifiers
Rules for Naming:
Must start with a letter or underscore.
Can contain letters, digits, and underscores (e.g.,
_age
,
count123
).
Cannot start with a digit or contain spaces.
C# is
case-sensitive
, meaning
myVar
and
MyVar
are treated as different identifiers.
Naming Conventions:
Camel Case:
camelCase
for local variables and method arguments.
Pascal Case:
PascalCase
for method names and public properties.
Avoid using reserved keywords (e.g.,
int
,
class
) as variable names.
3.
Literals and Type Suffixes
Numeric Literals:
Fixed values directly written in the code.
123
is an
int
literal by default.
123.45
is a
double
literal by default.
Suffixes to Specify Type:
f
for
float
(e.g.,
3.14f
)
d
for
double
(optional, as this is default for decimals, e.g.,
3.14d
)
m
for
decimal
(e.g.,
123.45m
)
u
for unsigned integers (e.g.,
uint x = 100u;
)
l
for
long
(e.g.,
10000000000L
)
4.
Type Conversions
Implicit Conversion:
Automatically done by the compiler if no data is lost.
Example:
int
to
double
,
float
to
double
.
Explicit Conversion (Casting):
Required when there is a risk of data loss.
Syntax:
(TARGET_TYPE) value
Example:
double d = 5.5; int i = (int)d; // i becomes 5
Parsing and Converting from
string
:
int.Parse("123")
converts
"123"
to
123
.
int.TryParse("123", out int result)
safely converts if possible; otherwise,
result
is
0
.
5.
Operators in C#
Arithmetic Operators:
+
,
-
,
*
,
/
,
%
Example:
int sum = 5 + 3; // sum is 8
Comparison Operators:
==
,
!=
,
>
,
<
,
>=
,
<=
Example:
bool isEqual = (5 == 3); // isEqual is false
Logical Operators:
&&
(AND),
||
(OR),
!
(NOT)
Example:
bool result = (5 > 3) && (3 > 1); // result is true
Assignment Operators:
=
,
+=
,
-=
,
*=
,
/=
,
%=
Example:
int x = 5; x += 2; // x is now 7
Operator Precedence:
Determines the order of operations in expressions.
Precedence Order (High to Low):
Multiplication and division (
*
,
/
,
%
) before addition and subtraction (
+
,
-
).
Relational and equality operators have lower precedence than arithmetic.
Logical operators have the lowest precedence.
Example:
int result = 2 + 3 * 4; // result is 14
6.
String Basics
Strings in C#:
Immutable sequences of characters.
Example:
string greeting = "Hello, World!";
String Immutability:
Once created, a string cannot be modified. Any modification creates a new string.
Common String Methods:
Concatenation:
string fullName = firstName + " " + lastName;
Length:
int length = greeting.Length;
Substring:
string sub = greeting.Substring(0, 5); // "Hello"
ToUpper/ToLower:
greeting.ToUpper(); // "HELLO, WORLD!"
7.
Boolean and Conditional Statements
Boolean Type:
bool
is used for true/false values.
Conditional Operators:
Comparison:
==
,
!=
,
>
,
<
,
>=
,
<=
Logical:
&&
,
||
,
!
Example:
if (x > 0 && y < 5) { /* do something */ }
Control Structures:
if
,
else if
, and
else
statements control the flow based on conditions.
Switch Statement:
Tests a variable against multiple values.
csharp
Copy code
switch (day) {
case 1: Console.WriteLine("Monday"); break;
case 2: Console.WriteLine("Tuesday"); break;
default: Console.WriteLine("Other Day"); break;
}
8.
Console Input and Output
Output to Console:
Console.WriteLine("Hello, World!");
Writes a line to the console.
Input from Console:
string input = Console.ReadLine();
Reads a line of text input from the console.
9.
Common Errors to Avoid
Uninitialized Variables:
Always initialize variables to prevent unexpected default values.
Type Mismatch:
Ensure the right types for operations (e.g.,
int
vs.
double
).
String Immutability:
Remember that any change to a string creates a new string, which can lead to inefficiency if repeated in loops.
Summary: Key Concepts for Week 1
Data Types:
Understand value types (e.g.,
int
,
float
,
bool
) vs. reference types (e.g.,
string
).
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
Ctrl
P
) instead.