I've been reading a book about C#. What does the word 'set' mean in the following excerpt?
Pattern matching with the switch statement: Like the if statement, the switch statement supports pattern matching in C# 7.0 and later. The case values no longer need to be literal values; they can be patterns. Let's see an example of pattern matching with the switch statement using a folder path. If you are using macOS, then swap the commented statement that sets the path variable and replace my username with your user folder name:
- Add the following statement to the top of the file to import types for working with input/output: using System.IO;
- Add statements to the end of the Main method to declare a string path to a file, open it as either a readonly or writeable stream, and then show a message based on what type and capabilities the stream has, as shown in the following code:
// string path ="/Users/markjprice/Code/Chapter03"; string path => @"C:\Code\Chapter03"; Write("Press R for readonly or W for write: "); ConsoleKeyInfo key = ReadKey(); WriteLine(); ...
My question is about the following part:
If you are using macOS, then swap the commented statement that sets the path variable and replace my username with your user folder name
Would anybody explain it to me?
This question is one of English semantics, not programming, which initially urged me to vote to close this question as being off topic.
However, because "set" is notoriously the word with the most numerous and widely ranging definitions in the English dictionary (link - 430 definitions in the OED, a whopping 60,000 word long entry), it seems on topic enough to focus on what it specifically means to a programmer when referring to code.
Homing in on the intention:
the [..] statement that sets the path variable
Very specifically, this means the following code (I assume the => was a typo on your part):
string path = @"C:\Code\Chapter03";
This statement sets a value (@"C:\Code\Chapter03"
) in the variable (path
).
That's really all there is to say here. "To set" means "to define a value".
You mean that "set" is kind of synonym to 'initialize'?
No.
Firstly, you declare a variable:
string path;
Note that this does not set a value (For those who disagree: I'm ignoring default values here - the compiler warns you about uninitialized variables even if the variable's type has a default value).
Then, when you first assign a value, that's what we call initialization:
string path; // declaration
path = "..."; // initialization
// OR
string path = "..."; // declaration + initialization
However, "setting" a value happens any time you change the value of the variable. After the first time, it's no longer called initialization; but it is still "setting" the value.
All initializations are also inherently a case of setting a value. But not all cases of a value being set are also an initialization.
string path;
path = "A";
path = "B";
path = "C";
The path
variable was initialized once but its value was set three times.
string path;
will result in initialization to null (but any assignment, even if multiple, inside the class constructor is also considered initialization). If inside a method, then string path;
is uninitialized and any attempt to read from it will result in the compiler loudly telling you so. — Feb 02, 2022 at 22:07 External links referenced by this document: