4

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:

  1. Add the following statement to the top of the file to import types for working with input/output: using System.IO;
  2. 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?

11
  • 2
    Please, next time, when you post code anywhere, invest some minimal formatting effort to make it readable. But FWIW: "set" can be replaced by "initializes", or "assigns [a certain value to]". And "the commented statement that sets the path variable" refers to the first line in the code block section above.
    – Doc Brown
    Feb 2 at 6:05
  • 1
    You mean that "set" is kind of synonym to 'initialize'?
    – Hossein
    Feb 2 at 6:41
  • 16
    I’m voting to close this question because it asks about the meaning of a word used in a sentence rather than a keyword in a programming language. Questions like this would be a better fit for the English Language Learners community. Feb 2 at 13:42
  • 4
    It just means to change the value of a variable to something, to "set its value". It's a verb, the opposite of get. (Later on, you'll learn about "getters" and "setters"). In case you're confusing it with the mathematical notion of a set (a collection of elements) - it has nothing to do with that; those are two different terms that happen to be written the same. Feb 2 at 14:49
  • 3
    @GregBurghardt: I am here with Flater, who gave a good reason why not to close this question.
    – Doc Brown
    Feb 2 at 14:56

2 Answers 2

33

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.

13
  • 17
    "To set" means "to define a value" - I think more formally we would equate it with the operation of assignment.
    – J...
    Feb 2 at 18:30
  • 1
    Setting is the same thing as assigning a value. You can assign a value to a variable more than once, or if you prefer, you can say that you have set the value. Feb 2 at 21:11
  • 1
    Re "means "to define a value".", "assign" or "give" make more sense than "define"
    – ikegami
    Feb 2 at 21:49
  • 1
    Re "in the variable", variables don't contain a value; they have a value. "of the variable" would make more sense.
    – ikegami
    Feb 2 at 21:51
  • 1
    @ikegami: If a class field member, 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.
    – Ben Voigt
    Feb 2 at 22:07
6

In this context, the word set means to assign a value to a variable.

This can correspond to several of the many dictionary definitions of the English word set, as a transitive verb.

  • to cause to assume a specified condition, relation, or occupation
    • slaves were set free → foreach (Slave s in slaves) s.IsFree = true;
    • set the house on fire → house.IsOnFire = true;
  • to cause to assume a specified posture or position
    • set the door ajar → door.Position = AJAR;
  • to adjust (a device and especially a measuring device) to a desired position
    • set the alarm for 7:00 → alarm.Time = new TimeOfDay(7, 0);
    • set a thermostat at 68 → thermostat.Temperature = 68;
  • to fix at a certain amount
    • set bail at $500 → bail = 500.00;

The C# language in particular often uses the keywords get and set in combination to define what it calls a property.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.