In this article, I will be explaining C# data types using an example and an explanation of how it works. Before we start, in case you want to learn more about C#, this wiki may be useful.
Contents
- Separating characters from one phone number (1st program)
- Separating characters from four phone numbers (2nd program)
- Another example use
- Summary
- Conclusion
First, what are C# data types? All languages have data types, and each type is for storing different types of info. Some common data types are: string, int, char, and bool.
One way to think of data types is like storing different types of things in boxes because, you could store items such as cords, books, and art supplies in a properly shaped box or you could use ones that would likely result in a tight arrangement even with careful packing.
Another analogy that might help: drinks because, there are different types and possible amounts; a cup of coffee or hot chocolate (data type) can go in a thin handle-less cup (data type storage) but…picking that cup up might be difficult and so a mug might be better, lemonade can work in most cups but, looks good in a glass but then there’s the amount — do you have a lot or a little relative to the cup.
C# Data types are similar because, different data types (like the types of objects) are best stored in the format that works for it — whole numbers (AKA integers) in an int variable, characters in a char variable, etc. Numbers and words are a good example, “9” and “keyboard” can both be stored in a string variable but, it would likely be more difficult to use numbers in a string format for a calculator type of program so, we use an int. Data type sizes don’t usually matter, especially in basic programs but, like with drinks, different data can come in different amounts — a table below shows some comparisons.
C# | C | C++ | JS | Python | |
Bool | Yes | Yes | Yes | Yes | Yes |
Char | Yes | Yes | Yes | Yes | Yes |
String | Yes | No | Yes | Yes | Yes |
Int (Numeric) | Yes | Yes | Yes | Yes | Yes |
Float (Numeric) | Yes | Yes | Yes | Yes | Yes |
Double (Numeric) | Yes | Yes | Yes | Yes | Yes |
C# | C | C++ | |
Bool | 1 bit | N/A | 1 byte |
Char | 2 bytes | 1 byte | 1 byte |
String | 2 bytes per character | N/A | might depend on # of characters |
Int (Numeric) | 4 bytes | 2 or 4 bytes | 4 bytes |
Float (Numeric) | 4 bytes | 4 | 4 bytes |
Double (Numeric) | 8 bytes | 8 | 8 bytes |
The programs listed in this article take a phone take a phone number as the input and then it will separate the parenthesis, numbers, spaces, dash, and new line character into the data types that they go in (assuming that anything that’s not a number is a string).
Our code on C# data types:
The Main method:
using System;
using System.Collections.Generic;//Possibly necessary to make lists work
namespace DataTypesProg
{
class Program
{
static void Main(string[] args)
{
//The rest of our code goes here
}
}
}
Declaring and initializing some variables and taking input:
//The string variables
string char_check, check_for_num_write, check_for_other_write;
//A character array to store the individual characters of a phone number
char[] new_array = new char[14];
Console.WriteLine("Enter a number: ");
string input = Console.ReadLine();//Taking input
char[] input_array = new char[input.Length];//A character array to store the sepearte chacaters from the input
Processing our input:
for (int i = 0; i < input.Length; i++)//Seperating the characters of the input into the 'input_array'
{
input_array[i] = input[i];
}
for (int i = 0; i < input_array.Length; i++)//A for-loop to check each character from the input now in 'input_array'
{
char_check = input_array[i].ToString();
//Seperating out the non-number characters
if (char_check == "(" || char_check == ")" || char_check == "-")
{
new_array[i] = input_array[i];
char_check = char_check.Replace(input_array[i], 'g');
//The line above replaces the non-numbers with a character that can be checked for and skipped when writing out the result
input_array[i] = char.Parse(char_check);//Storing the now checked through character from the input
}
//Checking for if the character is a space or a new line separately cause didn't work otherwise
if (char_check == " " || char_check == "\t")
{
char_check = char_check.Replace(input_array[i], 'g');
input_array[i] = char.Parse(char_check);
}
}
Writing out the stored characters:
//Writing out just the number characters
Console.Write("Just the numbers: ");
for (int z = 0; z < input_array.Length; z++)
{
check_for_num_write = input_array[z].ToString();
//Checking if the character is a number and writing it out, separating it with a comma
if (check_for_num_write == "1" || check_for_num_write == "2" || check_for_num_write == "3" || check_for_num_write == "4" || check_for_num_write == "5" || check_for_num_write == "6" || check_for_num_write == "7" || check_for_num_write == "8" || check_for_num_write == "9" || check_for_num_write == "0")
{
Console.Write(input_array[z]);
if (z < input_array.Length - 1)
{
Console.Write(", ");
}
else if (z == (input_array.Length) - 1)//If the for-loops is incrementing value = the length of the array, all has been written out and it's time for a new line
{
Console.WriteLine("\n");
}
}
}
//Writing out the non-number characters
Console.Write("New string output: ");
Console.WriteLine("\n");
for (int otherchar = 0; otherchar < new_array.Length; otherchar++)
{
check_for_other_write = new_array[otherchar].ToString();
if (check_for_other_write == "(" || check_for_other_write == ")" || check_for_other_write == "-")
{
Console.Write(new_array[otherchar]);
if (otherchar < new_array.Length - 1)//Displaying the characters on their own line
{
Console.WriteLine("\n");
}
}
}
Output
Enter a number:
(567) 123-3673
Just the numbers: 5, 6, 7, 1, 2, 3, 3, 6, 7, 3
New string output:
(
)
-
The way this program works is:
First, we make the string variables, char_check, check_for_num_write, and check_for_other_write
. The first one (char_check
) is used for storing one of the characters from the input of a phone number, for making it possible to check whether it’s a number or not, so that it can be separated accordingly.
The second, check_for_num_write,
we use to temporarily store a character from the array that has non-number characters removed, to determine when to write out a character from the array based on it being a number or not.
The last one (check_for_other_write
) works similarly to the last. A few lines down, we make a character array (new_array
), and this we use to store the individual characters from the input that the person types.
Then it’s time to prompt for input.
After that, we make another character array (input_array
), this one with size (AKA the length) of the phone number that was entered so that each character that was entered has a spot. It’s probably best to make this array at this point because, the aspect of its length isn’t necessarily known until the phone number is entered because, although we could assume the phone number to be entered with a specific number of characters, that may not happen each time.
Then we use a for-loop to copy each character from the input into input_array
(the array we just made) which is useful because, being an array, each spot has an index (i.e. an address) that can be iterated through to check the type of character in that spot.
Now, we can use a for-loop to iterate through the indexes of input_array
and check the character type. In the for-loop, first, we convert the character at the input_array
index value equal to the for-loops iteration value (in this case “i”, which we set to 0 at the beginning of the for-loop) to a string and place that string result in our temporary string char_check
. For the steps that follow, it’s important to convert the index to a string because, that makes it possible for the if statements to read it and determine what type of character it is (again, the way this program works is it considers numbers integers and everything else a string).
In the processing of the characters using if statements, it may be best if we check for the possibility of the character being a parenthesis or a dash, separately from the possibility of it being a space or a new line because, spaces and new line characters might be read differently by the computer, which could cause unexpected results.
If the character at the index that’s being checked turns out to be a parenthesis, dash, space, or a new line character, then it gets copied to new_array
(our array for non-number characters) and replaced with a ‘g’ character in input_array
(our array for the original input). Replacing the non-number characters with a isn’t necessary but, it can help us debug where the program is messing up, if that happens, by being a clear way to see where the program has found a non-number character and sorted it.
Here it may be more appropriate to use the string.Remove
function but, for this version of the code we’ll use string.Replace
it since it can be simpler (more of the reasoning is listed after the summary, later on).
After separating the different characters, we write out the two categories of characters that are now in separate arrays using one last for-loop for each category.
There is a difference in how the two categories are written out to the screen but, for both the values at the individual indexes of the array storing the characters are converted to a string so that an if statement can check them (because the string.Replace
function replaced the characters with a “g”), and if the character is written out if it is in fact a number. For the non-number characters, the difference is that each character is separated by a line because, writing them out like this can be simpler.
A question that you may have about this section is why, when writing out the characters, is are we checking when to write the comma or new line based on if the for-loop’s iteration value is at the second to last index of the array being written out and that’s because, the comma is meant to be written before the last character in the array being displayed.
Modifying the C# data types program to find the most common number from multiple phone numbers:
The ultra basics of our program
using System;
using System.Collections.Generic;//Possibly necessary to make lists work
namespace DataTypesProg
{
class Program
{
static void Main(string[] args)
{
//Enter code here
}
}
}
Declaring and initializing some variables:
string char_check, check_for_num_write, check_for_other_write;
string[] four_numbers = new string[4];//An array to store 4 numbers together, each as one value of the array
string temp_1num_holding;//String for storing one of the phone numbers from 'four_numbers'
//variables to represent the categories of numbers between 0-9, 0 being in the last
int n2 = 0, n4 = 0, n6 = 0, n8 = 0, n10 = 0;//Have to defined to be able to be used
string all_num_chars = "";//Declaring all_num_chars with a starting value so that it can be used -- up here so that it only gets defines once, and not over and over during the main for-loop
char[] new_array = new char[14];//An array to store the individual characters from one phone number
//An extra line
Processing our input four times (inside a new for-loop):
for (int four_num = 0; four_num < 4; four_num++)//Loop to take input of 4 numbers
{
Console.WriteLine("Enter a number: ");
four_numbers[four_num] = Console.ReadLine();
temp_1num_holding = four_numbers[four_num];//Transferring one phone number from 'four_numbers' to a temporary one to be able to process just one phone number
//Now using the length of the temporary (temp_1num_holding) array to store the characters of one phone number
char[] input_array = new char[temp_1num_holding.Length];//Makes an array of the size of the input, smart
//Copying each character of one phone number into an array to actually process that phone number
for (int i = 0; i < temp_1num_holding.Length; i++)
{
input_array[i] = temp_1num_holding[i];
new_array[i] = ' ';//re-defining new_array, so that old values don't stick arround
}
for (int i = 0; i < input_array.Length; i++)//Filtering the characters of one phone number
{
char_check = input_array[i].ToString();
//Rpobably could re-define new_array here too
if (char_check == "(" || char_check == ")" || char_check == "-")
{
/*Maybe what happens is new_array is written to with the first number,
* then with the new number becasue there's not the same amount of non-nmubers
* (missing the dash) new_array isn't fully updated.
* So, new_array needs to be re-defined earlier,
* probably at the beginning of the main for-loop.
* Though, to go through each of the spots -- because it's an array -- may take a for-loop.
*/
new_array[i] = input_array[i];
//Console.WriteLine(new_array);//Just displaying new_array as a check
char_check = char_check.Replace(input_array[i], 'g');
input_array[i] = char.Parse(char_check);
//Console.WriteLine(new_array);//Just displaying new_array as a check
}
if (char_check == " " || char_check == "\t")//Checking for if the character is a space or a new line separately cause didn't work otherwise
{
char_check = char_check.Replace(input_array[i], 'g');
input_array[i] = char.Parse(char_check);
//Console.WriteLine(new_array);//Just displaying new_array as a check
}
}
//Writing out the number characters
Console.Write("Just the numbers: ");
for (int z = 0; z < input_array.Length; z++)
{
check_for_num_write = input_array[z].ToString();
if (check_for_num_write == "1" || check_for_num_write == "2" || check_for_num_write == "3" || check_for_num_write == "4" || check_for_num_write == "5" || check_for_num_write == "6" || check_for_num_write == "7" || check_for_num_write == "8" || check_for_num_write == "9" || check_for_num_write == "0")
{
Console.Write(input_array[z]);
if (z != input_array.Length - 1)//Displaying a comma in between each number
{
Console.Write(", ");
all_num_chars = all_num_chars + check_for_num_write;//Saving each of the numbers from the phone numbers to a string
}
else if (z == (input_array.Length) - 1)//If all the numbers are displayed, make a new line
{
Console.WriteLine("\n");
all_num_chars = all_num_chars + check_for_num_write; //Saving each of the numbers from the phone numbers to a string
}
}
}
Console.WriteLine("all the number characters: " + all_num_chars);//Just writing out all_num_chars to make sure it's working
//Writing out the non-numbers
Console.Write("The non-numbers: ");
Console.WriteLine("\n");
for (int otherchar = 0; otherchar < new_array.Length; otherchar++)
{
check_for_other_write = new_array[otherchar].ToString();
if (check_for_other_write == "(" || check_for_other_write == ")" || check_for_other_write == "-")
{
Console.Write(new_array[otherchar]);
if (otherchar != new_array.Length - 1)//Displaying the characters on their own line
{
Console.WriteLine("\n");
}
}
}
}
//The rest of the code goes after this point
Displaying all the phone numbers:
//Loop that displays the four stored phone numbers (in the format they were entered)
for (int allnum = 0; allnum < 4; allnum++)
{
Console.WriteLine("Displaying the stored phone numbers: " + four_numbers[allnum]);
}
Counting the occurrences of numbers:
//Counting the amount each number occured
for (int l = 0; l < all_num_chars.Length; l++)
{
if (all_num_chars[l] == '1' || all_num_chars[l] == '2')
{
n2++;
}
if (all_num_chars[l] == '3' || all_num_chars[l] == '4')
{
n4++;
}
if (all_num_chars[l] == '5' || all_num_chars[l] == '6')
{
n6++;
}
if (all_num_chars[l] == '7' || all_num_chars[l] == '8')
{
n8++;
}
if (all_num_chars[l] == '9' || all_num_chars[l] == '0')
{
n10++;
}
}
Console.WriteLine("all the number characters: " + all_num_chars);//Displaying only the number characters from all the phone numbers
//An extra line
Displaying the most common number character:
//Displaying the amount each number occured
if (n2 > n4 && n2 > n6 && n2 > n8 && n2 > n10)
{
Console.WriteLine("The most common characters are 1 or 2");
Console.WriteLine("There were " + n2 + " 1 or 2s");
}
if (n2 < n4 && n4 > n6 && n4 > n8 && n4 > n10)
{
Console.WriteLine("The most common characters are 3 or 4");
Console.WriteLine("There were " + n4 + " 3 or 4s");
}
if (n6 > n4 && n2 < n6 && n6 > n8 && n6 > n10)
{
Console.WriteLine("The most common characters are 5 or 6");
Console.WriteLine("There were " + n6 + " 5 or 6s");
}
if (n2 < n8 && n8 > n4 && n8 > n6 && n8 > n10)
{
Console.WriteLine("The most common characters are 7 or 8");
Console.WriteLine("There were " + n8 + " 7 or 8s");
}
if (n2 < n10 && n10 > n4 && n10 > n6 && n10 > n8)
{
Console.WriteLine("The most common characters are 9 or 0");
Console.WriteLine("There were " + n10 + " 9 or 0s");
}
else
{
Console.WriteLine("All the characters occur the same amount");
}
//An extra line
Output
Enter a number:
(100) 000-0000
Just the numbers: 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
all the numbers: 1000000000
New string output:
(
)
-
Enter a number:
(100) 000 0000
Just the numbers: 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
all the numbers: 10000000001000000000
New string output:
(
)
Enter a number:
(100) 000-0000
Just the numbers: 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
all the numbers: 100000000010000000001000000000
New string output:
(
)
-
Enter a number:
(100) 000-0000
Just the numbers: 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
all the numbers: 1000000000100000000010000000001000000000
New string output:
(
)
-
Displaying the accumulated y: (100) 000-0000
Displaying the accumulated y: (100) 000 0000
Displaying the accumulated y: (100) 000-0000
Displaying the accumulated y: (100) 000-0000
all the numbers: 1000000000100000000010000000001000000000
The most common characters are 9 or 0
There were 36 9 or 0s
An explanation of this version of the code:
In short, we take the code from the other version and modify it work within a for-loop that runs four times thus, making it possible to process multiple phone numbers.
The differences between the other version are:
- The new string array
four_numbers
to store phone numbers that are prompted for four times - The new string
temp_1num_holding
for temporally storing one of the four phone numbers so that it can be processed into its individual characters - I define
input_array
usingtemp_1num_holding
‘s length the inside the new all encompassing for-loop, - I take the characters from
temp_1num_holding
and transfer them to theinput_array
like I did wit theinput
string from the other version of the code
Another example of how this C# program could be used:
- Reformatting phone numbers such as going from +1 (934) 395-8970 to +1 9343958970
Summary
To wrap up, to better understand C# data types, we made two programs that process the characters from a phone number into the categories of integers and strings (again, assuming that anything that’s not a number is a string). The first program only takes one phone number as its input and, the second program adds to this by using a for-loop that runs the code from the first program a set number of times (in this case four times), and then another for-loop takes the now separated number characters and tallies the number of times the numbers occur across all four phone numbers.
Areas for improvement:
- In both of my programs, I could check for the number characters instead of the non-numbers, and this would make it possible for the program to work even if the phone numbers entered have a different format — such as not having separating characters.
- There are likely spots where I could’ve simplified the use of multiple variables down to just one, such as possibly checking each character of from the
input
string and separating them into the appropriate character arrays, removing the step of separating the characters of the input into a character array that then gets processed. - Lists may have been more appropriate instead of character arrays, which would’ve removed the need to define the size of the character arrays.
string.Remove
may have been more appropriate instead ofstring.Replace
but, this may have added the complexity of having to redefine the size of the array that holds the new amount of characters because, removing a character from an array doesn’t automatically update the length of the array to a lower value (but this may have worked if I used lists).- Using functions could help organize the code, thus making it easier to debug the code.
Conclusion
Thank you for reading this article on C# data types, I hope you enjoyed and learned from it, and if you did, our C#, C++, C, or other archives may interest you. If you have any questions or comments, feel free to post them below, and we’ll do our best to reply.