SlideShare a Scribd company logo
Core C# Programming
Constructs, Part 1
Lecturer: Vahid Farahmandian
https://www.linkedin.com/in/vfarahmandian
The Anatomy of a Simple C# Program
C# demands that all program logic be contained within a type
definition
Every executable C# application (console program, Windows desktop
program, or Windows service) must contain a class defining a Main()
method
Main() method signify the entry point of the application
The class that defines the Main() method is termed as application
object
Each executable can have multiple application object
Variations on the Main( ) Method
With the release of C# 7.1, the
Main() method can now be async
Specifying an Application Error Code
The ability to return an int from Main() keeps C# consistent with other
C-based languages
Returning 0 Program has terminated successfully
Returning other Represents an error condition
Value 0 is automatically returned even if Main() method declared as
void
In Windows OS, an application return value stored in a system
environment variable called %ERRORLEVEL%
Processing Command-Line Arguments
you are able to access command-line arguments using the static
GetCommandLineArgs() method of the System.Environment type.
Using GetCommandLineArgs() it is no longer necessary to define
Main() as taking a string array parameter
The return value of this method is an array of strings. The first index
identifies the name of the application itself,
while the remaining elements in the array contain the individual
command-line arguments
You can define command-line args via Project Properties/Debug
section too
System Data Types and Corresponding C#
Keywords
The C# data type keywords are actually shorthand notations for full-
blown types in the System namespace
Among the data types, these types are not CLS Compliant:
sbyte  -128 – 128  Signed 8-bit number
ushort  0 – 65,535  Unsigned 16-bit number
uint  0 – 4,294,967,295  Unsigned 32-bit number
ulong  0 – 18,446,744,073,709,551,615 Unsigned 64-bit number
If you expose non-CLS-compliant data from your programs, other
.NET languages might not be able to make use of it.
Variable Declaration and Initialization
Variables are declared as: DataType Name
Make use of a local variable before assigning an initial value will cause
compiler error
You can declare multiple variables of same type in one line of code
The default literal is a new feature in C# 7.1 that allows for assigning a
variable the default value for its data type
Intrinsic Data Types and the new Operator
All intrinsic data types support what is known as a default constructor.
This feature allows you to create a variable using the new keyword,
which automatically sets the variable to its default value
bool variables are set to false.
Numeric data is set to 0 (or 0.0 in the case of floating-point data types).
char variables are set to a single empty character.
BigInteger variables are set to 0.
DateTime variables are set to 1/1/0001 12:00:00 AM.
Object references (including strings) are set to null.
The Data Type Class Hierarchy
The Data Type Class Hierarchy
Each type ultimately derives from System.Object
many numerical data types derive from a class named
System.ValueType
Descendants of ValueType are automatically allocated on the stack
types that do not have System.ValueType in their inheritance chain are
not allocated on the stack but on the garbage-collected heap
Members of Numerical Data Types
numerical types of .NET support MaxValue and MinValue properties
that provide information regarding the range a given type can store
System.Double type allows you to obtain the values for epsilon and
infinity
Members of System.Boolean
can take value from the set {true | false}
System.Boolean does not support a MinValue/MaxValue property
but rather support TrueString/FalseString
Members of System.Char
C# textual data is represented by the string and char keywords, both of
which are Unicode under hood
Using the static methods of System.Char, you are able to determine
whether a given character is numerical, alphabetical, a point of
punctuation, or whatnot
Parsing Values from String Data
The .NET data types provide the ability to generate a variable of their
underlying type given a textual equivalent
One issue with the preceding code is that an exception will be thrown
if the string cannot be cleanly converted to the correct data type
Instead of Parse you can use TryParse statement takes an out
parameter and returns a bool if the parsing was successful.
System.DateTime and System.TimeSpan
The System namespace defines a few useful data types for which there
are no C# keywords, such as the DateTime and TimeSpan structures
The DateTime type contains data that represents a specific date (month,
day, year) and time value, both of which may be formatted in a variety
of ways using the supplied members.
The TimeSpan structure allows you to easily define and transform units
of time using various members.
The System.Numerics.dll Assembly
The System.Numerics namespace defines a structure named BigInteger,
etc
BigInteger data type can be used when you need to represent
humongous numerical values, which are not constrained by a fixed
upper or lower limit
when you define a literal whole number (such as 500), the runtime will
default the data type to an int. Likewise, literal floating-point data
(such as 55.333) will default to a double
Digit Separators
C# 7 introduces the underscore (_) as a digit separator (for integer,
long, decimal, or double data types).
The new digit separator also works with binary literals
Binary Literals
C# 7 introduces a new literal for binary values, for example, for
creating bit masks.
Working with String Data
String Concatenation
string variables can be connected to build larger strings via the C# +
(as well as +=) operator.
It is possible to perform string concatenation by calling String.Concat()
directly
although you really have not gained anything by doing so—in fact, you
have incurred additional keystrokes!).
Escape Characters
Defining Verbatim Strings
When you prefix a string literal with the @ symbol, you have created
what is termed a verbatim string.
Using verbatim strings, you disable the processing of a literal’s escape
characters and print out a string as is.
Using verbatim strings, you can also directly insert a double quote into
a literal string by doubling the " token.
Strings and Equality
When you perform a test for equality on reference types (via the C# ==
and != operators), you will be returned true if the references are
pointing to the same object in memory
However, even though the string data type is indeed a reference type,
the equality operators have been redefined to compare the values of
string objects, not the object in memory to which they refer.
You are able to test for equality using the Equals() method of String as
well as the baked-in equality operators
Modifying String Comparison Behavior
The string equality operators (Compare(), Equals(), and ==) as well as
the IndexOf() function are by default case-sensitive and culture-
insensitive
This can cause a problem if your program doesn’t care about case
One way to overcome this is to convert everything to uppercase or
lowercase and then compare, This makes a copy of each string with all
lowercase letters.
A much better practice is to use the overloads of the methods listed
earlier that take a value of the StringComparison enumeration to
control exactly how the comparisons are done
Modifying String Comparison Behavior
Strings Are Immutable
After you assign a string object with its initial value, the character data
cannot be changed
In fact the methods of the string type are, in fact returning you a new
string object is a modified format
The string class can be inefficient and result in bloated code if misused,
especially when performing string concatenation or working with huge
amounts of text data
The System.Text.StringBuilder Type
Like the System.String class, the StringBuilder defines methods that allow
you to replace or format segments
When you call members of this type, you are directly modifying the object’s
internal character data, not obtaining a copy of the data in a modified format
By default, a StringBuilder is only able to initially hold a string of 16
characters or fewer
Default starting value can be changed via an additional constructor argument
If you append more characters than the specified limit, the StringBuilder
object will copy its data into a new instance and grow the buffer by the
specified limit
String Interpolation
Starting with the release of C# 6, C# programmers can use an
alternative syntax to build string literals that contain placeholders for
variables. Formally, this is called string interpolation
While the output of the operation is identical to traditional string
formatting syntax, this new approach allows you to directly embed the
variables themselves, rather than tacking them on as a comma-
delimited list.
Narrowing and Widening Data Type
Conversions
Widening is the term used to define an implicit upward cast that does
not result in a loss of data.
Narrowing is the logical opposite of widening, in that a larger value is
stored within a smaller data type variable.
The CLR was unable to apply a narrowing operation
All narrowing conversions result in a compiler error
EXCEPT When you want to inform the compiler that you are willing
to deal with a possible loss of data because of a narrowing operation,
you must apply an explicit cast using the C# casting operator, ().
The checked Keyword
When you wrap a statement (or a block of statements) within the scope
of the checked keyword, the C# compiler emits additional CIL
instructions that test for overflow conditions that may result when
adding, multiplying, subtracting, or dividing two numerical data types.
You can activate project-wide checking
via Project Property/Build/Advanced
The unchecked Keyword
When you activate the project-wide overflow/underflow checking, you
can use unchecked keyword to make a overflow/underflow acceptable
Understanding Implicitly Typed Local
Variables
C# language does provide for implicitly typing of local variables using
the var keyword.
When you do so, the compiler will automatically infer the underlying
data type based on the initial value used to initialize the local data
point
implicit typing applies only to local variables in a method or property
scope
It is illegal to use the var keyword to define return values, parameters,
or field data of a custom type
Understanding Implicitly Typed Local
Variables
local variables declared with the var keyword must be assigned an
initial value at the exact time of declaration and cannot be assigned the
initial value of null
Understanding Implicitly Typed Local
Variables
Implicit typing of local variables results in strongly typed data
Therefore, use of the var keyword is not the same technique used with
scripting languages (such as JavaScript), where a variable can hold
values of different types over its lifetime in a program (often termed
dynamic typing).
C# does allow for dynamic typing in C# using a keyword called:
dynamic.
The LINQ technology set makes use of query expressions that can
yield dynamically created result sets based on the format of the query
itself. In these cases, implicit typing is extremely helpful
C# Iteration Constructs
for loop
foreach/in loop
while loop
do/while loop
The for Loop
When you need to iterate over a block of code a fixed number of times,
the for statement provides a good deal of flexibility
You can create complex terminating conditions, build endless loops,
loop in reverse (via the -- operator), and use the goto, continue, and
break jump keywords
The foreach Loop
The C# foreach keyword allows you to iterate over all items in a
container without the need to test for an upper limit
Unlike a for loop, however, the foreach loop will walk the container
only in a linear (n+1) fashion
It is also possible to use implicit typing within a foreach looping
construct
The while and do/while Looping Constructs
The while looping construct is useful should you want to execute a
block of statements until some terminating condition has been reached
Like a simple while loop, do/while is used when you need to perform
some action an undetermined number of times. The difference is that
do/while loops are guaranteed to execute the corresponding block of
code at least once.
Decision Constructs and the Relational/Equality
Operators
The if/else statement
The switch statement
The if/else Statement
Unlike in C and C++, the if/else statement in C# operates only on
Boolean expressions, not ad hoc values such as –1 or 0.
Equality and Relational Operators
The Conditional Operator
The conditional operator (?:) is a shorthand method of writing a simple
if-else statement
both types of first expression and second expression must be the same
the conditional operator can be used only in assignment statements.
Logical Operators
An if statement may be composed of complex expressions as well and
can contain else statements to perform more complex testing
The && and || operators both “short-circuit” when necessary
The switch Statement
the switch statement allows you to handle program flow based on a
predefined set of choices.
C# demands that each case (including default) that contains executable
statements have a terminating return, break, or goto to avoid falling
through to the next statement.
case statements can be combined to share common code
the switch statement also supports using a goto to exit a case condition
and execute another case statement. While this is supported, it’s
generally thought of as an anti-pattern and not generally used.
Using Pattern Matching in Switch Statements
Prior to C# 7, match expressions in switch statements were limited to
comparing a variable to constant values, sometimes referred to as the
constant pattern
In C# 7, switch statements can also employ the type pattern, where
case statements can evaluate the type of the variable being checked
and case expressions are no longer limited to constant values
The rule that each case statement must be terminated with a return or
break still applies; however, goto statements are not supported using
the type pattern
Using Pattern Matching in Switch Statements
Using Pattern Matching in Switch Statements
This adds a new dimension to the switch statement as the order of the
case statements is now significant.
With the constant pattern, each case statement had to be unique. With
the type pattern, this is no longer the case.
The following code will produce compile time error, as the second case
has already been handled by the first case
Ad

Recommended

PDF
Functions in c++
Asaye Dilbo
 
PDF
Input and output in c++
Asaye Dilbo
 
PPTX
Concept of c data types
Manisha Keim
 
PPTX
C# String
Raghuveer Guthikonda
 
PDF
Introduction to c++
Prof. Dr. K. Adisesha
 
PPTX
C++
k v
 
PPT
Data Handling
Praveen M Jigajinni
 
PPTX
Sharbani bhattacharya VB Structures
Sharbani Bhattacharya
 
PPS
C programming session 02
Dushmanta Nath
 
PDF
Pointers
Swarup Kumar Boro
 
PPT
C++ data types
pratikborsadiya
 
PPTX
Basic Data Types in C++
Hridoy Bepari
 
PPTX
Chapter 2.datatypes and operators
Jasleen Kaur (Chandigarh University)
 
DOCX
Dot net programming concept
sandeshjadhav28
 
PDF
Strings-Computer programming
nmahi96
 
PPT
Getting started with c++
K Durga Prasad
 
PPTX
Datatype in c++ unit 3 -topic 2
MOHIT TOMAR
 
PPT
02a fundamental c++ types, arithmetic
Manzoor ALam
 
PDF
2 expressions (ppt-2) in C++
Kuntal Bhowmick
 
PDF
C programming | Class 8 | III Term
Andrew Raj
 
PPTX
Cpu-fundamental of C
Suchit Patel
 
PPS
C programming session 05
Dushmanta Nath
 
PPS
C programming session 09
Dushmanta Nath
 
PPTX
C data types, arrays and structs
Saad Sheikh
 
DOC
Comp 122 lab 7 lab report and source code
pradesigali1
 
PPT
Data type in c
thirumalaikumar3
 
PDF
Pointers-Computer programming
nmahi96
 
PDF
434090527-C-Cheat-Sheet. pdf C# program
MAHESHV559910
 
PDF
CSharpCheatSheetV1.pdf
ssusera0bb35
 
PPTX
CSharp Language Overview Part 1
Hossein Zahed
 

More Related Content

What's hot (19)

PPS
C programming session 02
Dushmanta Nath
 
PDF
Pointers
Swarup Kumar Boro
 
PPT
C++ data types
pratikborsadiya
 
PPTX
Basic Data Types in C++
Hridoy Bepari
 
PPTX
Chapter 2.datatypes and operators
Jasleen Kaur (Chandigarh University)
 
DOCX
Dot net programming concept
sandeshjadhav28
 
PDF
Strings-Computer programming
nmahi96
 
PPT
Getting started with c++
K Durga Prasad
 
PPTX
Datatype in c++ unit 3 -topic 2
MOHIT TOMAR
 
PPT
02a fundamental c++ types, arithmetic
Manzoor ALam
 
PDF
2 expressions (ppt-2) in C++
Kuntal Bhowmick
 
PDF
C programming | Class 8 | III Term
Andrew Raj
 
PPTX
Cpu-fundamental of C
Suchit Patel
 
PPS
C programming session 05
Dushmanta Nath
 
PPS
C programming session 09
Dushmanta Nath
 
PPTX
C data types, arrays and structs
Saad Sheikh
 
DOC
Comp 122 lab 7 lab report and source code
pradesigali1
 
PPT
Data type in c
thirumalaikumar3
 
PDF
Pointers-Computer programming
nmahi96
 
C programming session 02
Dushmanta Nath
 
C++ data types
pratikborsadiya
 
Basic Data Types in C++
Hridoy Bepari
 
Chapter 2.datatypes and operators
Jasleen Kaur (Chandigarh University)
 
Dot net programming concept
sandeshjadhav28
 
Strings-Computer programming
nmahi96
 
Getting started with c++
K Durga Prasad
 
Datatype in c++ unit 3 -topic 2
MOHIT TOMAR
 
02a fundamental c++ types, arithmetic
Manzoor ALam
 
2 expressions (ppt-2) in C++
Kuntal Bhowmick
 
C programming | Class 8 | III Term
Andrew Raj
 
Cpu-fundamental of C
Suchit Patel
 
C programming session 05
Dushmanta Nath
 
C programming session 09
Dushmanta Nath
 
C data types, arrays and structs
Saad Sheikh
 
Comp 122 lab 7 lab report and source code
pradesigali1
 
Data type in c
thirumalaikumar3
 
Pointers-Computer programming
nmahi96
 

Similar to Core C# Programming Constructs, Part 1 (20)

PDF
434090527-C-Cheat-Sheet. pdf C# program
MAHESHV559910
 
PDF
CSharpCheatSheetV1.pdf
ssusera0bb35
 
PPTX
CSharp Language Overview Part 1
Hossein Zahed
 
PPTX
CS4443 - Modern Programming Language - I Lecture (2)
Dilawar Khan
 
PPTX
C# overview part 1
sagaroceanic11
 
PPTX
02. Primitive Data Types and Variables
Intro C# Book
 
PPT
02 Primitive data types and variables
maznabili
 
PDF
C# Language Overview Part I
Doncho Minkov
 
PPTX
Lesson 4 Basic Programming Constructs.pptx
John Burca
 
PPT
C Sharp Jn (1)
jahanullah
 
PPT
C Sharp Nagina (1)
guest58c84c
 
PDF
C# Data Types and Comments_Lecture2_C#.pdf
Azhar Babil
 
PPTX
Getting started with C# Programming
Bhushan Mulmule
 
PPTX
C# slid
pacatarpit
 
PPTX
data types in C-Sharp (C#)
Abid Kohistani
 
PPTX
2 programming with c# i
siragezeynu
 
PPT
Introduction To C#
SAMIR BHOGAYTA
 
PPTX
C# lecture 2: Literals , Variables and Data Types in C#
Dr.Neeraj Kumar Pandey
 
PPT
Introduction to C#
ANURAG SINGH
 
PPTX
19csharp
Sireesh K
 
434090527-C-Cheat-Sheet. pdf C# program
MAHESHV559910
 
CSharpCheatSheetV1.pdf
ssusera0bb35
 
CSharp Language Overview Part 1
Hossein Zahed
 
CS4443 - Modern Programming Language - I Lecture (2)
Dilawar Khan
 
C# overview part 1
sagaroceanic11
 
02. Primitive Data Types and Variables
Intro C# Book
 
02 Primitive data types and variables
maznabili
 
C# Language Overview Part I
Doncho Minkov
 
Lesson 4 Basic Programming Constructs.pptx
John Burca
 
C Sharp Jn (1)
jahanullah
 
C Sharp Nagina (1)
guest58c84c
 
C# Data Types and Comments_Lecture2_C#.pdf
Azhar Babil
 
Getting started with C# Programming
Bhushan Mulmule
 
C# slid
pacatarpit
 
data types in C-Sharp (C#)
Abid Kohistani
 
2 programming with c# i
siragezeynu
 
Introduction To C#
SAMIR BHOGAYTA
 
C# lecture 2: Literals , Variables and Data Types in C#
Dr.Neeraj Kumar Pandey
 
Introduction to C#
ANURAG SINGH
 
19csharp
Sireesh K
 
Ad

More from Vahid Farahmandian (8)

PPTX
Building C# Applications
Vahid Farahmandian
 
PPTX
The Philosophy of .Net
Vahid Farahmandian
 
PPTX
C#.net evolution part 2
Vahid Farahmandian
 
PPTX
Understanding Security in .Net Framework
Vahid Farahmandian
 
PPTX
Introduction to SOA
Vahid Farahmandian
 
PPTX
Introduction to CRM
Vahid Farahmandian
 
PPTX
C#.net Evolution part 1
Vahid Farahmandian
 
PPTX
Interview tips
Vahid Farahmandian
 
Building C# Applications
Vahid Farahmandian
 
The Philosophy of .Net
Vahid Farahmandian
 
C#.net evolution part 2
Vahid Farahmandian
 
Understanding Security in .Net Framework
Vahid Farahmandian
 
Introduction to SOA
Vahid Farahmandian
 
Introduction to CRM
Vahid Farahmandian
 
C#.net Evolution part 1
Vahid Farahmandian
 
Interview tips
Vahid Farahmandian
 
Ad

Recently uploaded (20)

PDF
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
mary rojas
 
PDF
Humans vs AI Call Agents - Qcall.ai's Special Report
Udit Goenka
 
PPTX
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
 
PDF
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
PDF
From Data Preparation to Inference: How Alluxio Speeds Up AI
Alluxio, Inc.
 
PDF
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
WSO2
 
PDF
Decipher SEO Solutions for your startup needs.
mathai2
 
PPTX
declaration of Variables and constants.pptx
meemee7378
 
PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
DOCX
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
 
PPTX
Sap basis role in public cloud in s/4hana.pptx
htmlprogrammer987
 
PPTX
Top Time Tracking Solutions for Accountants
oliviareed320
 
PPTX
AI for PV: Development and Governance for a Regulated Industry
Biologit
 
PDF
Which Hiring Management Tools Offer the Best ROI?
HireME
 
DOCX
Zoho Creator Solution for EI by Elsner Technologies.docx
Elsner Technologies Pvt. Ltd.
 
PDF
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
 
PPTX
Simplify Insurance Regulations with Compliance Management Software
Insurance Tech Services
 
PDF
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
PPTX
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
PDF
Building Geospatial Data Warehouse for GIS by GIS with FME
Safe Software
 
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
mary rojas
 
Humans vs AI Call Agents - Qcall.ai's Special Report
Udit Goenka
 
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
 
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
From Data Preparation to Inference: How Alluxio Speeds Up AI
Alluxio, Inc.
 
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
WSO2
 
Decipher SEO Solutions for your startup needs.
mathai2
 
declaration of Variables and constants.pptx
meemee7378
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
 
Sap basis role in public cloud in s/4hana.pptx
htmlprogrammer987
 
Top Time Tracking Solutions for Accountants
oliviareed320
 
AI for PV: Development and Governance for a Regulated Industry
Biologit
 
Which Hiring Management Tools Offer the Best ROI?
HireME
 
Zoho Creator Solution for EI by Elsner Technologies.docx
Elsner Technologies Pvt. Ltd.
 
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
 
Simplify Insurance Regulations with Compliance Management Software
Insurance Tech Services
 
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
Building Geospatial Data Warehouse for GIS by GIS with FME
Safe Software
 

Core C# Programming Constructs, Part 1

  • 1. Core C# Programming Constructs, Part 1 Lecturer: Vahid Farahmandian https://www.linkedin.com/in/vfarahmandian
  • 2. The Anatomy of a Simple C# Program C# demands that all program logic be contained within a type definition Every executable C# application (console program, Windows desktop program, or Windows service) must contain a class defining a Main() method Main() method signify the entry point of the application The class that defines the Main() method is termed as application object Each executable can have multiple application object
  • 3. Variations on the Main( ) Method With the release of C# 7.1, the Main() method can now be async
  • 4. Specifying an Application Error Code The ability to return an int from Main() keeps C# consistent with other C-based languages Returning 0 Program has terminated successfully Returning other Represents an error condition Value 0 is automatically returned even if Main() method declared as void In Windows OS, an application return value stored in a system environment variable called %ERRORLEVEL%
  • 5. Processing Command-Line Arguments you are able to access command-line arguments using the static GetCommandLineArgs() method of the System.Environment type. Using GetCommandLineArgs() it is no longer necessary to define Main() as taking a string array parameter The return value of this method is an array of strings. The first index identifies the name of the application itself, while the remaining elements in the array contain the individual command-line arguments You can define command-line args via Project Properties/Debug section too
  • 6. System Data Types and Corresponding C# Keywords The C# data type keywords are actually shorthand notations for full- blown types in the System namespace Among the data types, these types are not CLS Compliant: sbyte  -128 – 128  Signed 8-bit number ushort  0 – 65,535  Unsigned 16-bit number uint  0 – 4,294,967,295  Unsigned 32-bit number ulong  0 – 18,446,744,073,709,551,615 Unsigned 64-bit number If you expose non-CLS-compliant data from your programs, other .NET languages might not be able to make use of it.
  • 7. Variable Declaration and Initialization Variables are declared as: DataType Name Make use of a local variable before assigning an initial value will cause compiler error You can declare multiple variables of same type in one line of code The default literal is a new feature in C# 7.1 that allows for assigning a variable the default value for its data type
  • 8. Intrinsic Data Types and the new Operator All intrinsic data types support what is known as a default constructor. This feature allows you to create a variable using the new keyword, which automatically sets the variable to its default value bool variables are set to false. Numeric data is set to 0 (or 0.0 in the case of floating-point data types). char variables are set to a single empty character. BigInteger variables are set to 0. DateTime variables are set to 1/1/0001 12:00:00 AM. Object references (including strings) are set to null.
  • 9. The Data Type Class Hierarchy
  • 10. The Data Type Class Hierarchy Each type ultimately derives from System.Object many numerical data types derive from a class named System.ValueType Descendants of ValueType are automatically allocated on the stack types that do not have System.ValueType in their inheritance chain are not allocated on the stack but on the garbage-collected heap
  • 11. Members of Numerical Data Types numerical types of .NET support MaxValue and MinValue properties that provide information regarding the range a given type can store System.Double type allows you to obtain the values for epsilon and infinity
  • 12. Members of System.Boolean can take value from the set {true | false} System.Boolean does not support a MinValue/MaxValue property but rather support TrueString/FalseString
  • 13. Members of System.Char C# textual data is represented by the string and char keywords, both of which are Unicode under hood Using the static methods of System.Char, you are able to determine whether a given character is numerical, alphabetical, a point of punctuation, or whatnot
  • 14. Parsing Values from String Data The .NET data types provide the ability to generate a variable of their underlying type given a textual equivalent One issue with the preceding code is that an exception will be thrown if the string cannot be cleanly converted to the correct data type Instead of Parse you can use TryParse statement takes an out parameter and returns a bool if the parsing was successful.
  • 15. System.DateTime and System.TimeSpan The System namespace defines a few useful data types for which there are no C# keywords, such as the DateTime and TimeSpan structures The DateTime type contains data that represents a specific date (month, day, year) and time value, both of which may be formatted in a variety of ways using the supplied members. The TimeSpan structure allows you to easily define and transform units of time using various members.
  • 16. The System.Numerics.dll Assembly The System.Numerics namespace defines a structure named BigInteger, etc BigInteger data type can be used when you need to represent humongous numerical values, which are not constrained by a fixed upper or lower limit when you define a literal whole number (such as 500), the runtime will default the data type to an int. Likewise, literal floating-point data (such as 55.333) will default to a double
  • 17. Digit Separators C# 7 introduces the underscore (_) as a digit separator (for integer, long, decimal, or double data types). The new digit separator also works with binary literals
  • 18. Binary Literals C# 7 introduces a new literal for binary values, for example, for creating bit masks.
  • 20. String Concatenation string variables can be connected to build larger strings via the C# + (as well as +=) operator. It is possible to perform string concatenation by calling String.Concat() directly although you really have not gained anything by doing so—in fact, you have incurred additional keystrokes!).
  • 22. Defining Verbatim Strings When you prefix a string literal with the @ symbol, you have created what is termed a verbatim string. Using verbatim strings, you disable the processing of a literal’s escape characters and print out a string as is. Using verbatim strings, you can also directly insert a double quote into a literal string by doubling the " token.
  • 23. Strings and Equality When you perform a test for equality on reference types (via the C# == and != operators), you will be returned true if the references are pointing to the same object in memory However, even though the string data type is indeed a reference type, the equality operators have been redefined to compare the values of string objects, not the object in memory to which they refer. You are able to test for equality using the Equals() method of String as well as the baked-in equality operators
  • 24. Modifying String Comparison Behavior The string equality operators (Compare(), Equals(), and ==) as well as the IndexOf() function are by default case-sensitive and culture- insensitive This can cause a problem if your program doesn’t care about case One way to overcome this is to convert everything to uppercase or lowercase and then compare, This makes a copy of each string with all lowercase letters. A much better practice is to use the overloads of the methods listed earlier that take a value of the StringComparison enumeration to control exactly how the comparisons are done
  • 26. Strings Are Immutable After you assign a string object with its initial value, the character data cannot be changed In fact the methods of the string type are, in fact returning you a new string object is a modified format The string class can be inefficient and result in bloated code if misused, especially when performing string concatenation or working with huge amounts of text data
  • 27. The System.Text.StringBuilder Type Like the System.String class, the StringBuilder defines methods that allow you to replace or format segments When you call members of this type, you are directly modifying the object’s internal character data, not obtaining a copy of the data in a modified format By default, a StringBuilder is only able to initially hold a string of 16 characters or fewer Default starting value can be changed via an additional constructor argument If you append more characters than the specified limit, the StringBuilder object will copy its data into a new instance and grow the buffer by the specified limit
  • 28. String Interpolation Starting with the release of C# 6, C# programmers can use an alternative syntax to build string literals that contain placeholders for variables. Formally, this is called string interpolation While the output of the operation is identical to traditional string formatting syntax, this new approach allows you to directly embed the variables themselves, rather than tacking them on as a comma- delimited list.
  • 29. Narrowing and Widening Data Type Conversions Widening is the term used to define an implicit upward cast that does not result in a loss of data. Narrowing is the logical opposite of widening, in that a larger value is stored within a smaller data type variable. The CLR was unable to apply a narrowing operation All narrowing conversions result in a compiler error EXCEPT When you want to inform the compiler that you are willing to deal with a possible loss of data because of a narrowing operation, you must apply an explicit cast using the C# casting operator, ().
  • 30. The checked Keyword When you wrap a statement (or a block of statements) within the scope of the checked keyword, the C# compiler emits additional CIL instructions that test for overflow conditions that may result when adding, multiplying, subtracting, or dividing two numerical data types. You can activate project-wide checking via Project Property/Build/Advanced
  • 31. The unchecked Keyword When you activate the project-wide overflow/underflow checking, you can use unchecked keyword to make a overflow/underflow acceptable
  • 32. Understanding Implicitly Typed Local Variables C# language does provide for implicitly typing of local variables using the var keyword. When you do so, the compiler will automatically infer the underlying data type based on the initial value used to initialize the local data point implicit typing applies only to local variables in a method or property scope It is illegal to use the var keyword to define return values, parameters, or field data of a custom type
  • 33. Understanding Implicitly Typed Local Variables local variables declared with the var keyword must be assigned an initial value at the exact time of declaration and cannot be assigned the initial value of null
  • 34. Understanding Implicitly Typed Local Variables Implicit typing of local variables results in strongly typed data Therefore, use of the var keyword is not the same technique used with scripting languages (such as JavaScript), where a variable can hold values of different types over its lifetime in a program (often termed dynamic typing). C# does allow for dynamic typing in C# using a keyword called: dynamic. The LINQ technology set makes use of query expressions that can yield dynamically created result sets based on the format of the query itself. In these cases, implicit typing is extremely helpful
  • 35. C# Iteration Constructs for loop foreach/in loop while loop do/while loop
  • 36. The for Loop When you need to iterate over a block of code a fixed number of times, the for statement provides a good deal of flexibility You can create complex terminating conditions, build endless loops, loop in reverse (via the -- operator), and use the goto, continue, and break jump keywords
  • 37. The foreach Loop The C# foreach keyword allows you to iterate over all items in a container without the need to test for an upper limit Unlike a for loop, however, the foreach loop will walk the container only in a linear (n+1) fashion It is also possible to use implicit typing within a foreach looping construct
  • 38. The while and do/while Looping Constructs The while looping construct is useful should you want to execute a block of statements until some terminating condition has been reached Like a simple while loop, do/while is used when you need to perform some action an undetermined number of times. The difference is that do/while loops are guaranteed to execute the corresponding block of code at least once.
  • 39. Decision Constructs and the Relational/Equality Operators The if/else statement The switch statement
  • 40. The if/else Statement Unlike in C and C++, the if/else statement in C# operates only on Boolean expressions, not ad hoc values such as –1 or 0.
  • 42. The Conditional Operator The conditional operator (?:) is a shorthand method of writing a simple if-else statement both types of first expression and second expression must be the same the conditional operator can be used only in assignment statements.
  • 43. Logical Operators An if statement may be composed of complex expressions as well and can contain else statements to perform more complex testing The && and || operators both “short-circuit” when necessary
  • 44. The switch Statement the switch statement allows you to handle program flow based on a predefined set of choices. C# demands that each case (including default) that contains executable statements have a terminating return, break, or goto to avoid falling through to the next statement. case statements can be combined to share common code the switch statement also supports using a goto to exit a case condition and execute another case statement. While this is supported, it’s generally thought of as an anti-pattern and not generally used.
  • 45. Using Pattern Matching in Switch Statements Prior to C# 7, match expressions in switch statements were limited to comparing a variable to constant values, sometimes referred to as the constant pattern In C# 7, switch statements can also employ the type pattern, where case statements can evaluate the type of the variable being checked and case expressions are no longer limited to constant values The rule that each case statement must be terminated with a return or break still applies; however, goto statements are not supported using the type pattern
  • 46. Using Pattern Matching in Switch Statements
  • 47. Using Pattern Matching in Switch Statements This adds a new dimension to the switch statement as the order of the case statements is now significant. With the constant pattern, each case statement had to be unique. With the type pattern, this is no longer the case. The following code will produce compile time error, as the second case has already been handled by the first case