<jupyter_start><jupyter_text>Store and retrieve data using literal and variable values in CSource: https://docs.microsoft.com/en-us/learn/modules/csharp-literals-variables/Many applications you'll build in C will require you to work with data. Sometimes that data will be hard-coded in your application. For example, you may need to print a message to the user when some operation succeeds. A "success" message would likely be the same every time the application is executed.But more often, you'll work with data that is unknown as you build your application. Perhaps the data is captured via keyboard input, or comes from a file or the internet. Therefore, you'll need to build structures called variables that can store data in memory as your application runs.Suppose you want to display a formatted message to the end user containing different types of data. The message would include hard-coded strings, but may combine it with information your application collects from the user. To display a formatted message, you'll need to create both hard-coded values and define variables that can store data of a certain type, whether numeric, alphanumeric, and so on.In this module, you'll create hard-coded literal values that contain different data types. You'll create variables that can hold certain data types, set those variables with a value, then retrieve those values later in code. And finally, you'll learn how you can simplify your code by allowing the compiler to shoulder some of the work.By the end of this module, you'll be able to create literal values and store and retrieve data in variables. Learning objectivesIn this module, you will:* Create literal values for five basic data types* Declare and initialize variables* Retrieve and set values in variables* Allow the compiler to determine the data type for your variable when initializing ___ Literal Values What is a literal value?A literal value is a hard-coded value that never changes. Previously, we displayed a literal string to the Output pane. In other words, we literally wanted that string of alphanumeric characters `H`, `e`, `l`, `l`, `o`, and so on, displayed in the Output window.The string data type is used whenever you have alphanumeric words, phrases, or data for presentation, not calculation. More about that in a moment.What other kinds of literal data can we print to the Output pane? Print out different literal data typesThere are many data types in C. But as you're getting started you only need to know about five or six data types since they cover most scenarios. Let's display a literal instance of data type to the output pane. ***Note***```You may notice as you begin to work in the code window that it colors certain syntax in different colors to indicate keywords, operators, data types and more. Begin to take notice of the colors. It can help you spot syntax errors as you enter characters, and can help you understand the code more effectively.```If we only wanted a single alphanumeric character printed to screen, we could create a **char literal** by surrounding one alphanumeric character in single-quotes.<jupyter_code>Console.WriteLine('b');<jupyter_output><empty_output><jupyter_text>But If you enter the following code, You would get the following error:<jupyter_code>Console.WriteLine('Hello World!');<jupyter_output>(1,19): error CS1012: Too many characters in character literal<jupyter_text>The C compiler was expecting a single character (since you used the character literal syntax) but you supplied 12 characters instead!Just like the string data type, you use char whenever you have a single alphanumeric character for presentation (not calculation).The term char is short for character. In C, they're officially referred to as "char", but frequently referred to as a "character".  Write an int literal to the consoleIf you want to print a numeric whole number (no fractions) value to Output, you can use an `int` literal. An int literal requires no additional operators like the `string` or `char`.Add the following line of code in the code editor:<jupyter_code>Console.WriteLine(123);<jupyter_output>123<jupyter_text>The term int is short for integer, which you may recognize from studying math. In C, they're officially referred to as `"int"`, but frequently known by their alter ego `"integer"`.  Write a decimal literal to the consoleIf we wanted to print a number that includes values after the decimal point, we could use a **`decimal literal`**.To create a decimal literal, append the letter **`m`** after the number. In this context, the **`m`** is called a literal suffix. The literal suffix tells the compiler you wish to work with a value of type decimal.Add the following line of code in the code editor:<jupyter_code>Console.WriteLine(12.3m);<jupyter_output>12.3<jupyter_text>**Note**You can use either a lower-case **`m`** or upper-case **`M`** as the literal suffix for a decimal. Write a bool literal to the consoleIf we wanted to print a value representing either true or false, we could use a **`bool literal`**.Add the following lines of code in the code editor:<jupyter_code>Console.WriteLine(true);
Console.WriteLine(false);<jupyter_output>True
False<jupyter_text>The term bool is short for boolean, which you may also recognize from studying math. In C, they're officially referred to as "bool", but often developers use the term "boolean".The bool literals represent the idea of truthhood and falsehood. We'll use bool values extensively when we start to add decision logic to our applications. We'll evaluate expressions to see whether the expression is true or false.  Why emphasize data types?Data types play a central role in C. In fact, the emphasis on data types is one of the key distinguishing features of C compared to other languages like Python and JavaScript. The designers of C believed they can help developers avoid common software bugs by enforcing data types. You'll see this concept unfold as you learn more about C.Presentation versus calculation and evaluationEarlier we said that `strings` and `chars` are used for "presentation, not calculation". If you need to perform a mathematical operation on numeric values, you should use an `int` or `decimal`. If you have data that is used for presentation or reference purposes only, you should use a string or char data type.Suppose you needed to collect data from a user like a phone number or postal code. Depending on the country where you live, that data may consist of numeric characters. However, since you rarely perform mathematical calculations on phone numbers and postal codes, you should prefer to use a string data type when working with them.The same can be said of `bool`. If you need to work with the words `"true"` and `"false"` in your application, you would use a `string`. However, if you need to work with the concept of `true` or `false` when performing an evaluation, you use a bool. This should become clearer as we perform evaluations in other modules.It's important to know that these values may look like their string literal equivalents. In other words, you may think these statements are the same:<jupyter_code>Console.WriteLine("123");
Console.WriteLine(123);

Console.WriteLine("true");
Console.WriteLine(true);<jupyter_output>123
123
true
True<jupyter_text>RecapThe main takeaway is that there are many data types, but we'll focus on just a few for now:* string for words, phrases, or any alphanumeric data for presentation, not calculation* char for a single alphanumeric character* int for a whole number* decimal for a number with a decimal* bool for a true/false value___ Declaring VariablesA literal is *literally* a hard-coded value. However, most applications will require us to work with values that we don't know much about ahead of time. In other words we'll need to work with data that comes from users, from files, or from across the network.When we need to work with data from outside of our code, we'll declare a variable. What is a variable?A **variable** is a data item that may change its value during its lifetime. You use variables to temporarily store values that you intend to use later in your code. A variable is a friendly label that we can assign to a computer memory address. When we want to temporarily store a value in that memory address, or whenever we want to retrieve the value that is stored in the memory address, we just use the variable name we created. Declaring a variableTo create a new variable, you must first declare the data type of the variable, then give it a name.<jupyter_code>string firstName;<jupyter_output><empty_output><jupyter_text>In this case, we're creating a new variable of type `string` called `firstName`. From now on, this variable can only hold string values.I can choose any name as long as it adheres to a few C syntax rules for naming variables. Variable name rules and conventionsA software developer once famously said "The hardest part of software development is naming things." Not only does the name of a variable have to follow certain syntax rules, it should also be used to make the code more human-readable and understandable. That's a lot to ask of one line of code!Here's a few important considerations about variable names:* Variable names can contain alphanumeric characters and the underscore character. Special characters like the hash symbol `` (also known as the number symbol or pound symbol) or dollar symbol `$` are not allowed.* Variable names must begin with an alphabetical letter or an underscore, not a number. Developers use the underscore for a special purpose, so try to not use that for now.* Variable names **must not be a C keyword**. For example, you cannot use the following variable declarations: `decimal` decimal; or `string` string;.* Variable names are case-sensitive, meaning that `string Value;` and `string value;` are two different variables.* Variable names should use `camel case`, which is a style of writing that uses a lower-case letter at the beginning of the first word and an upper-case letter at the beginning of each subsequent word. For example, `string thisIsCamelCase;`.* Variable names should be descriptive and meaningful in your application. Choose a name for your variable that represents the kind of data it will hold.* Variable names should be one or more entire words appended together. Don't use contractions because the name of the variable (and therefore, its purpose) may be unclear to others who are reading your code.* Variable names shouldn't include the data type of the variable. You might see some advice to use a style like `string strValue;`. That advice is no longer current.The example `string firstName;` follows all of these rules and conventions, assuming I want to use this variable to store data that represents someone's first name. Variable name examplesHere's a few examples of variable declarations using the data types we learned about previously.<jupyter_code>char userOption;

int gameScore;

decimal particlesPerMillion;

bool processedCustomer;<jupyter_output><empty_output><jupyter_text>RecapHere's the main takeaways you've learned so far about variables:* Variables are temporary values you store in the computer's memory.* Before you can use a variable, you have to declare it.* To declare a variable, you first select a data type for the kind of data you want to store, and then give the variable a name that follows the rules.Now that we know how to declare a variable, let's learn how to set, retrieve, and initialize the value of a variable.___  Implicitly typed local variablesThe C compiler affords many conveniences as you write your code. It can infer your variable's data type by its initialized value. In this unit, you'll learn about this feature, called implicitly typed local variables. What are implicitly typed local variables?An implicitly typed local variable is created using the `var` keyword, which instructs the C compiler to infer the type. Once the type is inferred, it's the same as if the actual data type had been used to declare the variable.In the following example, we'll declare a variable using the `var` keyword instead of the `string` keyword.<jupyter_code>var message = "Hello World!";<jupyter_output><empty_output><jupyter_text>Since the variable `message` is immediately set to the string value `"Hello World!"`, the C compiler understands the intent and treats every instance of message as an instance of type string.In fact, the `message` variable is typed to be a string and can never be changed. Here, we'll attempt to set `message` to the literal decimal value of `10.0m`.<jupyter_code>var message = "Hello World!";
message = 10.0m<jupyter_output>(2,11): error CS0029: Cannot implicitly convert type 'decimal' to 'string'<jupyter_text>You can only use the var keyword if the variable is initialized.It's important to understand that the `var` keyword is dependent on the value you use to initialize the variable. If you try to use the `var` keyword without initializing the variable, you'll receive an error when you attempt to compile your code.<jupyter_code>var message;<jupyter_output>(1,5): error CS0818: Implicitly-typed variables must be initialized<jupyter_text>Why use the var keyword?The `var` keyword has been widely adopted in the C community, so it's likely if you look at a code example in a book or online, you'll see the var keyword used instead of the actual data type name. So, we wanted to make sure to introduce it in this module.However, the `var` keyword has an important use in C. For reasons that may not be clear to you until you write advanced code, there are situations where the data type may not be obvious at the time you initialize the variable. In fact, in some cases, C may invent a new data type just for your code and may not be able to give it a predictable name ahead of time. Again, this is an advanced feature of C that will be covered in other modules.As you get started, we recommend you continue to use the actual data type name when you declare variables. Using the data type when you declare variables will help you be purposeful as you write your code. RecapThe most important takeaways from this unit about the `var` keyword and implicitly type local variables:* The `var` keyword tells the compiler to infer the data type of the variable based on the value it is initialized to.* You'll likely see the `var` keyword as you read other people's code. However, you should use the data type when possible.<jupyter_code>string name = "Bob";
int inbox = 3;
decimal temperature = 34.4m;

Console.WriteLine($"Hello, {name}! You have {inbox} in your inbox.  The temperature is {temperature} celsius.");<jupyter_output>Hello, Bob! You have 3 in your inbox.  The temperature is 34.4 celsius.
{'fraction_non_alphanumeric': 0.05011512935121224, 'fraction_numerical': 0.0040633888663145065, 'mean_word_length': 4.990669371196755, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '180690', 'license': 'MIT', 'n_tokens_mistral': 3553, 'n_tokens_neox': 3393, 'n_words': 2281, 'path': '1_csharp-first-step/2_Store and retrieve data using literal and variable.ipynb', 'repo_name': 'BenedictusAryo/learn_csharp', 'size': '14766'}