1. Home
  2. Training Library
  3. DevOps
  4. Courses
  5. C# Data Types and Variables

DEMO: Arrays

Contents

keyboard_tab
Course Intro & Overview
1
Introduction
PREVIEW49s
2
Overview
PREVIEW1m 18s
Summary
Start course
Overview
Difficulty
Beginner
Duration
50m
Students
10
Ratings
5/5
starstarstarstarstar
Description

In this course, we look at how different types of data are stored using variables within a C# program. C# is a strongly typed language, meaning when you manipulate data in code, you must keep the data in variables that are specifically designed to hold that kind of data. For example, text is stored in a string data type and a letter in a char. There are over ten different numeric data types that vary in size and accuracy or precision of the data they can faithfully represent. We investigate some of the quirks in dealing with fractional numbers in a computer's binary environment. There are in-depth code examples for each of these topics to illustrate the discussed concepts and make you more familiar with C# programming in general.

This course builds upon the key concepts and examples covered in the Introduction to C# course. It includes guided demonstrations to give you practical knowledge of how to handle the concepts covered.

Learning Objectives

  • Understand what variables are and how they're stored
  • Learn about data types for storing and manipulating text values
  • Learn about the various data types for storing and manipulating whole and fractional numbers
  • Learn about variables for storing multiple values of the same data type

Intended Audience

This course is intended for anyone who has a basic understanding of C# and now wants to build upon that knowledge.

Prerequisites

This course carries on from our Introduction to C# course, so we suggest taking that one first if you haven't already done so.

 

Code examples used in demos on GitHub

https://github.com/cloudacademy/csharp-datatypes-variables

 

Transcript

The last topic I want to look at in this course is arrays. An array is a collection or group of variables all of the same type. Let's create another project called arrays. Again, I'll create that in Ubuntu using the .netnew command. Once .NET has created the project, I'll CD into the arrays directory and open Visual Studio Code, setting the working directory to the current one with the "." argument. I'll click Yes to add the assets necessary to build the Arrays application.

Remember, from the first course, I talked about args, an array of strings that are passed into the main procedure of a C# program. An array is declared by appending the data type with square brackets. Like args, I'm declaring an array of string called arrayOfStrings and initializing it with four elements, "how", "now", "brown", and "cow". An array must be initialized when declared, even if it is with no elements using empty curly braces.

Next, I'll declare an array of ints with 5 items and finally an empty array of chars. An array is not just a data storage structure but has properties that return information about the array as opposed to the data it contains. One of those properties is length. Using the length property, let's print out the size of each array with string interpolation. I've changed the color theme of VSCode from the previous demo, so the variables within the string are in a different color. This makes reading the code in quickly seeing what is a literal string and what is a variable much easier.

Now, let's print out all the elements of arrayOfStrings. The first array element is always accessed with the index zero, so the last element will be array length minus one; in this case, length 4 - 1 is 3. Next, I'll print the arrayOfInts elements starting with element one. The index for accessing an array element doesn't have to be a literal number. You can use a variable, even in the form of another array element. Here, I'm using arrayOfInts item 3, which contains the value 2, to access the third arrayOfStrings item. Yes, a little bit meta, but it illustrates the use of dynamic indexes and the zero index first element. Here we go, "how", "now", "brown", "cow".

Let's assign that third string element to a string variable called cowColor. ArrayOfLetters has been declared as empty, so it doesn't actually exist in memory yet. I'll create a new array of char with the same length as the cowColor variable by specifying the array size within square brackets after the data type.

Once the array exists, we can assign values to the elements, so the letter "c" to the first element. We can assign values to arrayOfLetters by accessing the cowColor string variable in the same way we access an array. Fundamentally a string is a special array of char. When I write out all arrayOfLetters elements, we essentially output a string. Arrays can be multi-dimensional, so two or more dimensions.

A two-dimensional array is analogous to a checker or chessboard, so you specify an element with two indexes, like coordinates. Declare a two-dimensional array using square brackets called checkers, but this time with a comma to separate the dimensions and assign a new int array specifying each dimension's length to be 4. I'll assign a value to each array element specifying the first dimension index and then the second separated by a comma.

Once all elements have a value, I'll print them all out, one dimension per line, and print a separating line before the checkerboard. Let's run that to see the checkerboard pattern printed to the console.

About the Author
Avatar
Hallam Webber
Software Architect
Students
11296
Courses
19
Learning Paths
1

Hallam is a software architect with over 20 years experience across a wide range of industries. He began his software career as a  Delphi/Interbase disciple but changed his allegiance to Microsoft with its deep and broad ecosystem. While Hallam has designed and crafted custom software utilizing web, mobile and desktop technologies, good quality reliable data is the key to a successful solution. The challenge of quickly turning data into useful information for digestion by humans and machines has led Hallam to specialize in database design and process automation. Showing customers how leverage new technology to change and improve their business processes is one of the key drivers keeping Hallam coming back to the keyboard. 

Covered Topics