Working with Variables: Strings
Learn about options for storing text in C# including literal strings, raw string literals, verbatim strings, and interpolated strings.
All applications process data. Data comes in, data is processed, and then data goes out. Data usually comes into our program from files, databases, or user input, and it can be put temporarily into variables that will be stored in the memory of the running program. When the program ends, the data in memory is lost. Data is usually output to files, databases, the screen, or a printer. When using variables, we should think about the following:
How much space does the variable take in the memory?
How fast can it be processed?
We control this by picking an appropriate type. We can think of simple common types such as int
and double
as different-sized storage boxes, where a smaller container would take less memory but may need to be faster at processing. For example, adding 16-bit numbers might take longer than adding 64-bit numbers on a 64-bit operating system. Some of these boxes may be stacked close by, and some may be thrown into a big heap further away.
Naming things and assigning values
There are naming conventions for things, and it is good practice to follow them, as shown in the following table:
Naming Convention | Examples | Used For |
Camel case | cost, orderDetail, dateOfBirth | Local variables, private fields. |
Title case aka Pascal case | String, Int32, Cost, DateOfBirth, Run | Types, non-private fields, and other members-like methods. |
Some C# programmers like to prefix the names of private fields with an underscore, ...