SlideShare a Scribd company logo
Learn C# Programming
Nullables & Arrays
Eng Teong Cheah
Microsoft MVP in Visual Studio &
Development Technologies
Agenda
•Nullables
•Arrays
•Strings
•Structure
•Enums
C# - Nullables
C# - Nullables
C# provides a special data types, the nullable types, to which you can
assign normal range of values as well as null values.
For example, you can store any value from -2,147,483,648 to 2,
147,483, 647 or null in a Nullable<Int32> variable. Similarly, you can
assign true, false, or null in a Nullable<bool> variable. Syntax for
declaring a nullable type is as follows:
< data_type> ? <variable_name> = null;
Demo
C# - Nullables
The Null Coalescing Operator(??)
The null coalescing operator is used with the nullable value types and
reference types. It is used for converting an operand to the type of
another nullable (or not) value type operand, where an implicit
conversion is possible.
Demo
C# - Arrays
C# - Arrays
An array stores a fixed-size sequential collection of elements of the
same type. An array is used to store a collection of data, but it is
often more useful to think of an array as a collection of variables of
the same type stored at contiguous memory locations.
Instead of declaring individual variables, such as number0, number1,
…, and number99, you declare one array variable such as numbers
and use numbers[0], numbers[1], and …, numbers[99] to represent
individual variables. A specific element in an array is accessed by an
index.
C# - Arrays
All arrays consist of contiguous memory locations. The lowest
address corresponds to the first element and the highest address to
the last element.
C# - Arrays
Declaring Arrays
To declare an array in C#, you can use the following syntax:
where,
datatype[] arrayName;
C# - Arrays
where,
- datatype is used to specify the type of elements in the array.
- [ ] specifies the rank of the array. The rank specifies the size of the
array.
- arrayName specifies the name of the array.
For example,
double[] balance;
Declaring Arrays
C# - Arrays
Initializing Arrays
Declaring an array does not initialize the array in the memory. When
the array variable is initialized, you can assign values to the array.
Array is a reference type, so you need to use the new keyword to
create an instance of the array. For example,
double[] balance = new double[10];
C# - Arrays
Assigning Values to an Array
You can assign values to individual array elements, by using the index
number, like:
You can assign values to the array at the time of declaration, as
shown:
double[] balance = new double[10];
balance[0] = 4500.0;
double[] balance = { 2340.0, 4523.69, 3421.0};
C# - Arrays
You can also create and initialize an array, as shown:
You may also omit the size of the array, as shown:
int [] marks = new int[5] { 99, 98, 92, 97, 95};
int [] marks = new int[] { 99, 98, 92, 97, 95};
C# - Arrays
You can copy an array variable into another target array variable. In
such case, both the target and source point to the same memory
location:
When you create an array, C# compiler implicitly initializes each array
element to a default value depending on the array type. For example,
for an int array all elements are initialized to 0.
int [] marks = new int[] { 99, 98, 92, 97, 95};
int[] score = marks;
C# - Arrays
Accessing Array Elements
An element is accessed by indexing the array name. This is done by
placing the index of the element within square brackets after the
name of the array. For example,
double salary = balance[9];
Demo
C# - Arrays
Using the foreach Loop
In the previous example, we used a for loop for accessing each array
element. You can also use a foreach statement to iterate through an
array.
Demo
C# - Arrays
There are following few important concepts related to array which
should be clear to a C# programmer:
- Multi-dimensional arrays
- Jagged arrays
- Passing arrays to functions
- Param arrays
- The Array Class
C# - Arrays
Multidimensional Arrays
C# allows multidimensional arrays. Multi-dimensional arrays are also
called rectangular array. You can declare a 2-dimensional array of
strings as:
or, a 3-dimensional array of int variables as:
string [,] names;
int [ , , ] m;
C# - Arrays
Multidimensional Arrays
Two-Dimensional Arrays
The simplest form of the multidimensional array is the 2-dimensional
array. A 2-dimensional array is a list of one-dimensional arrays.
C# - Arrays
A 2-dimensional array can be thought of as a table, which has x
number of rows and y number of columns. Following is a 2-
dimensional array, which contains 3 rows and 4 columns.
C# - Arrays
Thus, every element in the array a is identified by an element name
of the form a[i,j], where a is the name of the array, and i and j are the
subscripts that uniquely identify each element in array a.
C# - Arrays
Initializing Two-Dimensional Arrays
Multidimensional arrays may be initialized by specifying bracketed
values for each row. The following array is with 3 rows and each row
has 4 columns.
int [,] a = new int [3,4] {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
C# - Arrays
Accessing Two-Dimensional Array Elements
An element in 2-dimensional array is accessed by using the
subscripts. That is, row index and column index of the array. For
example,
The above statement takes 4th element form the 3rd row of the array.
You can verify it in the above diagram.
int val = a[2,3];
Demo
C# - Arrays
Jagged Arrays
A Jagged array is an arrays. You can declare a jagged array named
scores of type int as:
int [][] scores;
C# - Arrays
Jagged Arrays
Declaring an array, does not create the array in memory. To create
the above array:
int[][] scores = new int[5][];
for (int i = 0; i < scores.Length; i++)
{
scores[i] = new int[4];
}
C# - Arrays
Jagged Arrays
You can initialize a jagged array as:
Where, scores is an array of two arrays of integers – scores[0] is an
array of 3 integers and scores[1] is an array of 4 integers.
int[][] scores = new int[2][]{new int[]{92,93,94},new int[]{85,66,87,88}};
Demo
C# - Arrays
Passing Arrays as Function Arguments
You can pass an array as a function argument in C#.
Demo
C# - Arrays
Param Array
At times, while declaring a method, you are not sure of the number
of arguments passed as a parameter. C# param arrays (or parameter
arrays) come into help at such times.
Demo
C# - Arrays
Array Class
The Array class is the base class for all the arrays in C#. It is defined in
the System namespace. The Array class provides various properties
and methods to work with arrays.
C# - Arrays
Properties of the
Array Class
The following table
describes some of the
most commonly used
properties of the
Array class:
C# - Arrays
Methods of the Array
Class
The following table
describes some of the
most commonly used
methods of the Array
class:
C# - Arrays
Methods of the Array
Class
The following table
describes some of the
most commonly used
methods of the Array
class:
Related Content
•TutorialsPoint
www.tutorialspoint.com
Thank You

More Related Content

PDF
Learn C# Programming - Variables & Constants
Eng Teong Cheah
 
PDF
Learn C# Programming - Operators
Eng Teong Cheah
 
PDF
Learn C# programming - Program Structure & Basic Syntax
Eng Teong Cheah
 
PPTX
Introduction of C#
Eng Teong Cheah
 
PPT
pointer, structure ,union and intro to file handling
Rai University
 
PPTX
datatypes and variables in c language
Rai University
 
PPT
Declaration of variables
Maria Stella Solon
 
PDF
Python Programming
Saravanan T.M
 
Learn C# Programming - Variables & Constants
Eng Teong Cheah
 
Learn C# Programming - Operators
Eng Teong Cheah
 
Learn C# programming - Program Structure & Basic Syntax
Eng Teong Cheah
 
Introduction of C#
Eng Teong Cheah
 
pointer, structure ,union and intro to file handling
Rai University
 
datatypes and variables in c language
Rai University
 
Declaration of variables
Maria Stella Solon
 
Python Programming
Saravanan T.M
 

What's hot (20)

PPT
Variables in C Programming
programming9
 
PPTX
Structure of c_program_to_input_output
Anil Dutt
 
PPT
Constants in C Programming
programming9
 
PDF
Learn C# Programming - Decision Making & Loops
Eng Teong Cheah
 
PPTX
Variables in C and C++ Language
Way2itech
 
PPTX
What is identifier c programming
Rumman Ansari
 
PDF
Assignment2
Sunita Milind Dol
 
PDF
Basic of the C language
Sachin Verma
 
PPTX
C programming Training in Ambala ! Batra Computer Centre
jatin batra
 
PPTX
C tokens
Manu1325
 
PPT
Introduction to Basic C programming 01
Wingston
 
PPT
structure and union
student
 
PPTX
C Language (All Concept)
sachindane
 
PPS
Clanguage
Vidyacenter
 
PPTX
Unit 2. Elements of C
Ashim Lamichhane
 
PPTX
Basic Structure Of C++
DevangiParekh1
 
PPT
Ch2 introduction to c
Hattori Sidek
 
PDF
C# chap 4
Shehrevar Davierwala
 
PPT
Basics of C programming
avikdhupar
 
Variables in C Programming
programming9
 
Structure of c_program_to_input_output
Anil Dutt
 
Constants in C Programming
programming9
 
Learn C# Programming - Decision Making & Loops
Eng Teong Cheah
 
Variables in C and C++ Language
Way2itech
 
What is identifier c programming
Rumman Ansari
 
Assignment2
Sunita Milind Dol
 
Basic of the C language
Sachin Verma
 
C programming Training in Ambala ! Batra Computer Centre
jatin batra
 
C tokens
Manu1325
 
Introduction to Basic C programming 01
Wingston
 
structure and union
student
 
C Language (All Concept)
sachindane
 
Clanguage
Vidyacenter
 
Unit 2. Elements of C
Ashim Lamichhane
 
Basic Structure Of C++
DevangiParekh1
 
Ch2 introduction to c
Hattori Sidek
 
Basics of C programming
avikdhupar
 
Ad

Similar to Learn C# Programming - Nullables & Arrays (20)

DOC
Arrays In General
martha leon
 
PDF
Functions, Strings ,Storage classes in C
arshpreetkaur07
 
PDF
Unit ii data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
 
PPTX
Programming in c Arrays
janani thirupathi
 
PPTX
Programming in c arrays
Uma mohan
 
PDF
Arrays
Steven Wallach
 
PDF
Array and its types and it's implemented programming Final.pdf
ajajkhan16
 
PPTX
ARRAYS.pptx
MohammedtajuddinTaju
 
PPTX
C Programming Unit-3
Vikram Nandini
 
PPTX
Array.pptx
fixocin377
 
PPT
Arrays and vectors in Data Structure.ppt
mazanali7145
 
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
PPT
Algo>Arrays
Ain-ul-Moiz Khawaja
 
PPTX
Chapter-Five.pptx
berekethailu2
 
PPTX
Lecture 9
Mohammed Khan
 
PDF
Array and Collections in c#
Umar Farooq
 
PPTX
Array definition and uses in computer.pptx
naushigrdcs
 
PPT
Arrays
swathi reddy
 
Arrays In General
martha leon
 
Functions, Strings ,Storage classes in C
arshpreetkaur07
 
Unit ii data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
 
Programming in c Arrays
janani thirupathi
 
Programming in c arrays
Uma mohan
 
Array and its types and it's implemented programming Final.pdf
ajajkhan16
 
C Programming Unit-3
Vikram Nandini
 
Array.pptx
fixocin377
 
Arrays and vectors in Data Structure.ppt
mazanali7145
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
Algo>Arrays
Ain-ul-Moiz Khawaja
 
Chapter-Five.pptx
berekethailu2
 
Lecture 9
Mohammed Khan
 
Array and Collections in c#
Umar Farooq
 
Array definition and uses in computer.pptx
naushigrdcs
 
Arrays
swathi reddy
 
Ad

More from Eng Teong Cheah (20)

PDF
Modern Cross-Platform Apps with .NET MAUI
Eng Teong Cheah
 
PDF
Efficiently Removing Duplicates from a Sorted Array
Eng Teong Cheah
 
PDF
Monitoring Models
Eng Teong Cheah
 
PDF
Responsible Machine Learning
Eng Teong Cheah
 
PDF
Training Optimal Models
Eng Teong Cheah
 
PDF
Deploying Models
Eng Teong Cheah
 
PDF
Machine Learning Workflows
Eng Teong Cheah
 
PDF
Working with Compute
Eng Teong Cheah
 
PDF
Working with Data
Eng Teong Cheah
 
PDF
Experiments & TrainingModels
Eng Teong Cheah
 
PDF
Automated Machine Learning
Eng Teong Cheah
 
PDF
Getting Started with Azure Machine Learning
Eng Teong Cheah
 
PDF
Hacking Containers - Container Storage
Eng Teong Cheah
 
PDF
Hacking Containers - Looking at Cgroups
Eng Teong Cheah
 
PDF
Hacking Containers - Linux Containers
Eng Teong Cheah
 
PDF
Data Security - Storage Security
Eng Teong Cheah
 
PDF
Application Security- App security
Eng Teong Cheah
 
PDF
Application Security - Key Vault
Eng Teong Cheah
 
PDF
Compute Security - Container Security
Eng Teong Cheah
 
PDF
Compute Security - Host Security
Eng Teong Cheah
 
Modern Cross-Platform Apps with .NET MAUI
Eng Teong Cheah
 
Efficiently Removing Duplicates from a Sorted Array
Eng Teong Cheah
 
Monitoring Models
Eng Teong Cheah
 
Responsible Machine Learning
Eng Teong Cheah
 
Training Optimal Models
Eng Teong Cheah
 
Deploying Models
Eng Teong Cheah
 
Machine Learning Workflows
Eng Teong Cheah
 
Working with Compute
Eng Teong Cheah
 
Working with Data
Eng Teong Cheah
 
Experiments & TrainingModels
Eng Teong Cheah
 
Automated Machine Learning
Eng Teong Cheah
 
Getting Started with Azure Machine Learning
Eng Teong Cheah
 
Hacking Containers - Container Storage
Eng Teong Cheah
 
Hacking Containers - Looking at Cgroups
Eng Teong Cheah
 
Hacking Containers - Linux Containers
Eng Teong Cheah
 
Data Security - Storage Security
Eng Teong Cheah
 
Application Security- App security
Eng Teong Cheah
 
Application Security - Key Vault
Eng Teong Cheah
 
Compute Security - Container Security
Eng Teong Cheah
 
Compute Security - Host Security
Eng Teong Cheah
 

Recently uploaded (20)

PPT
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PPTX
Coupa-Overview _Assumptions presentation
annapureddyn
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
Architecture of the Future (09152021)
EdwardMeyman
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
This slide provides an overview Technology
mineshkharadi333
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Coupa-Overview _Assumptions presentation
annapureddyn
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
Architecture of the Future (09152021)
EdwardMeyman
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
Doc9.....................................
SofiaCollazos
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 

Learn C# Programming - Nullables & Arrays

  • 1. Learn C# Programming Nullables & Arrays Eng Teong Cheah Microsoft MVP in Visual Studio & Development Technologies
  • 4. C# - Nullables C# provides a special data types, the nullable types, to which you can assign normal range of values as well as null values. For example, you can store any value from -2,147,483,648 to 2, 147,483, 647 or null in a Nullable<Int32> variable. Similarly, you can assign true, false, or null in a Nullable<bool> variable. Syntax for declaring a nullable type is as follows: < data_type> ? <variable_name> = null;
  • 6. C# - Nullables The Null Coalescing Operator(??) The null coalescing operator is used with the nullable value types and reference types. It is used for converting an operand to the type of another nullable (or not) value type operand, where an implicit conversion is possible.
  • 9. C# - Arrays An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type stored at contiguous memory locations. Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.
  • 10. C# - Arrays All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
  • 11. C# - Arrays Declaring Arrays To declare an array in C#, you can use the following syntax: where, datatype[] arrayName;
  • 12. C# - Arrays where, - datatype is used to specify the type of elements in the array. - [ ] specifies the rank of the array. The rank specifies the size of the array. - arrayName specifies the name of the array. For example, double[] balance; Declaring Arrays
  • 13. C# - Arrays Initializing Arrays Declaring an array does not initialize the array in the memory. When the array variable is initialized, you can assign values to the array. Array is a reference type, so you need to use the new keyword to create an instance of the array. For example, double[] balance = new double[10];
  • 14. C# - Arrays Assigning Values to an Array You can assign values to individual array elements, by using the index number, like: You can assign values to the array at the time of declaration, as shown: double[] balance = new double[10]; balance[0] = 4500.0; double[] balance = { 2340.0, 4523.69, 3421.0};
  • 15. C# - Arrays You can also create and initialize an array, as shown: You may also omit the size of the array, as shown: int [] marks = new int[5] { 99, 98, 92, 97, 95}; int [] marks = new int[] { 99, 98, 92, 97, 95};
  • 16. C# - Arrays You can copy an array variable into another target array variable. In such case, both the target and source point to the same memory location: When you create an array, C# compiler implicitly initializes each array element to a default value depending on the array type. For example, for an int array all elements are initialized to 0. int [] marks = new int[] { 99, 98, 92, 97, 95}; int[] score = marks;
  • 17. C# - Arrays Accessing Array Elements An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example, double salary = balance[9];
  • 18. Demo
  • 19. C# - Arrays Using the foreach Loop In the previous example, we used a for loop for accessing each array element. You can also use a foreach statement to iterate through an array.
  • 20. Demo
  • 21. C# - Arrays There are following few important concepts related to array which should be clear to a C# programmer: - Multi-dimensional arrays - Jagged arrays - Passing arrays to functions - Param arrays - The Array Class
  • 22. C# - Arrays Multidimensional Arrays C# allows multidimensional arrays. Multi-dimensional arrays are also called rectangular array. You can declare a 2-dimensional array of strings as: or, a 3-dimensional array of int variables as: string [,] names; int [ , , ] m;
  • 23. C# - Arrays Multidimensional Arrays Two-Dimensional Arrays The simplest form of the multidimensional array is the 2-dimensional array. A 2-dimensional array is a list of one-dimensional arrays.
  • 24. C# - Arrays A 2-dimensional array can be thought of as a table, which has x number of rows and y number of columns. Following is a 2- dimensional array, which contains 3 rows and 4 columns.
  • 25. C# - Arrays Thus, every element in the array a is identified by an element name of the form a[i,j], where a is the name of the array, and i and j are the subscripts that uniquely identify each element in array a.
  • 26. C# - Arrays Initializing Two-Dimensional Arrays Multidimensional arrays may be initialized by specifying bracketed values for each row. The following array is with 3 rows and each row has 4 columns. int [,] a = new int [3,4] { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9, 10, 11} /* initializers for row indexed by 2 */ };
  • 27. C# - Arrays Accessing Two-Dimensional Array Elements An element in 2-dimensional array is accessed by using the subscripts. That is, row index and column index of the array. For example, The above statement takes 4th element form the 3rd row of the array. You can verify it in the above diagram. int val = a[2,3];
  • 28. Demo
  • 29. C# - Arrays Jagged Arrays A Jagged array is an arrays. You can declare a jagged array named scores of type int as: int [][] scores;
  • 30. C# - Arrays Jagged Arrays Declaring an array, does not create the array in memory. To create the above array: int[][] scores = new int[5][]; for (int i = 0; i < scores.Length; i++) { scores[i] = new int[4]; }
  • 31. C# - Arrays Jagged Arrays You can initialize a jagged array as: Where, scores is an array of two arrays of integers – scores[0] is an array of 3 integers and scores[1] is an array of 4 integers. int[][] scores = new int[2][]{new int[]{92,93,94},new int[]{85,66,87,88}};
  • 32. Demo
  • 33. C# - Arrays Passing Arrays as Function Arguments You can pass an array as a function argument in C#.
  • 34. Demo
  • 35. C# - Arrays Param Array At times, while declaring a method, you are not sure of the number of arguments passed as a parameter. C# param arrays (or parameter arrays) come into help at such times.
  • 36. Demo
  • 37. C# - Arrays Array Class The Array class is the base class for all the arrays in C#. It is defined in the System namespace. The Array class provides various properties and methods to work with arrays.
  • 38. C# - Arrays Properties of the Array Class The following table describes some of the most commonly used properties of the Array class:
  • 39. C# - Arrays Methods of the Array Class The following table describes some of the most commonly used methods of the Array class:
  • 40. C# - Arrays Methods of the Array Class The following table describes some of the most commonly used methods of the Array class: