|
| 1 | +## Declaring Variables, Constants, and Using `var` in DotNet |
| 2 | + |
| 3 | +This chapter focuses on the fundamentals of declaring variables and constants in DotNet, along with the use of the `var` keyword for implicit typing. Understanding these concepts is essential for writing clear, maintainable, and efficient code. We'll continue our journey of exploring DotNet through Test-Driven Development (TDD) by writing tests that illustrate these concepts using MSTest. |
| 4 | + |
| 5 | +### Variables in DotNet |
| 6 | + |
| 7 | +Variables are named storage locations that can hold a value of a specific type. In DotNet, you must declare a variable's type explicitly unless you use the `var` keyword for local type inference. |
| 8 | + |
| 9 | +### Constants in DotNet |
| 10 | + |
| 11 | +Constants are immutable values which, once defined, cannot be changed. Declaring a constant in DotNet is similar to declaring a variable, except you use the `const` keyword. Constants can make your code more readable and prevent magic numbers or strings from being scattered throughout your code. |
| 12 | + |
| 13 | +### The `var` Keyword |
| 14 | + |
| 15 | +The `var` keyword in DotNet allows for local type inference, meaning the compiler determines the type of the variable from the value it is assigned. Using `var` can make code more concise, especially when dealing with complex types. However, its use should be balanced with readability concerns. |
| 16 | + |
| 17 | +### Setting Up Tests for Variable and Constant Declarations |
| 18 | + |
| 19 | +To explore these concepts, we'll set up tests that demonstrate declaring and using variables and constants, and the use of `var` for local type inference. |
| 20 | + |
| 21 | +1. **Navigate to Your Test Project**: Make sure you're within the `MyFirstDotNetApp.Tests` project directory. |
| 22 | + |
| 23 | +2. **Create a Test Class for Variables and Constants**: For instance, you might create a test class named `VariableTests.cs` to contain your tests. |
| 24 | + |
| 25 | +### Writing Tests for Variables and Constants |
| 26 | + |
| 27 | +Let's write some tests to demonstrate declaring and using variables and constants. |
| 28 | + |
| 29 | +#### Testing Variable Declarations |
| 30 | + |
| 31 | +```csharp |
| 32 | +namespace MyFirstDotNetApp.Tests |
| 33 | +{ |
| 34 | + [TestClass] |
| 35 | + public class VariableTests |
| 36 | + { |
| 37 | + [TestMethod] |
| 38 | + public void VariableHoldsExpectedValue() |
| 39 | + { |
| 40 | + // Arrange |
| 41 | + int number = 5; |
| 42 | + |
| 43 | + // Act & Assert |
| 44 | + Assert.AreEqual(5, number); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +#### Testing Constant Declarations |
| 51 | + |
| 52 | +```csharp |
| 53 | +[TestMethod] |
| 54 | +public void ConstantHoldsExpectedValue() |
| 55 | +{ |
| 56 | + // Arrange |
| 57 | + const string greeting = "Hello, World!"; |
| 58 | + |
| 59 | + // Act & Assert |
| 60 | + Assert.AreEqual("Hello, World!", greeting); |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +### Testing the Use of `var` |
| 65 | + |
| 66 | +```csharp |
| 67 | +[TestMethod] |
| 68 | +public void VarInfersCorrectType() |
| 69 | +{ |
| 70 | + // Arrange |
| 71 | + var number = 5; // Should infer int |
| 72 | + var word = "test"; // Should infer string |
| 73 | +
|
| 74 | + // Act & Assert |
| 75 | + Assert.IsTrue(number is int); |
| 76 | + Assert.IsTrue(word is string); |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +### Implementing Code to Pass the Tests |
| 81 | + |
| 82 | +In these examples, the tests directly demonstrate the use of variables, constants, and `var` without needing additional implementation in the main project. These tests serve to illustrate how you can declare and initialize variables and constants and how the `var` keyword infers types based on assigned values. |
| 83 | + |
| 84 | +### Conclusion |
| 85 | + |
| 86 | +In this chapter, you've learned about declaring variables, constants, and using the `var` keyword for local type inference in DotNet. By employing TDD, you've seen how these concepts work in practice and how they can be tested. Understanding how to effectively use variables, constants, and `var` is foundational for any DotNet developer, enabling you to write more readable, maintainable, and efficient code. |
| 87 | + |
| 88 | +As you continue to explore DotNet, remember the importance of choosing the right approach for declaring types based on your specific needs. Whether you're defining constants to avoid magic numbers, explicitly typing variables for clarity, or using `var` for convenience, these tools are all essential parts of a DotNet developer's toolkit. |
0 commit comments