29

Update:
Surprised that it is being so heavily downvoted...

The question is coding-related and before asking this question I have googled for "42" in combination with:

  • site:msdn.micrsoft.com
  • "code example"
  • "c#"
  • "magic number"

And I am not an expert/fan of Western culture/literature.

Also found, Why are variables “i” and “j” used for counters? [duplicate] which was not closed but even protected.


I feel that everybody knows it, except me...

What is the origin of ubiquitous magic digit 42 used all over the code samples and samples?

How have you come using 42? because I have not ever come or ever used 42

After some search, I found MSDN doc on it: Magic Numbers: Integers:

  • "Aside from a book/movie reference, developers often use this as an arbitrary value"

Well, this did not explain me anything.

Which movies and books have I missed for all those years of being involved in development, coding and programming and around-IT related activities like rwquirements analysis, system administration, etc??

Some references to some texts using code snippets with 42 (just C#-related):

Jérôme Laban. C# Async Tips and Tricks, Part 3: Tasks and the Synchronization Context

  var t = Task.Delay(TimeSpan.FromSeconds(1))
              .ContinueWith
                (
                    _ => Task.Delay(TimeSpan.FromSeconds(42))
                );

MSDN Asynchronous Agents Library

   send(_target, 42);  

Quickstart: Calling asynchronous APIs in C# or Visual Basic

  Office.context.document.setSelectedDataAsync(
      "<html><body>hello world</body></html>",
       {coercionType: "html", asyncContext: 42},
       function(asyncResult) {
           write(asyncResult.status + " " + asyncResult.asyncContext);

Asynchronous Programming in C++ Using PPL

  task<int> myTask = someOtherTask.then([]() { return 42; });

Boxing and Unboxing (C# Programming Guide)

  Console.WriteLine(String.Concat("Answer", 42, true));

How To: Override the ToString Method (C# Programming Guide)

  int x = 42;

Trace Listeners

  // Use this example when debugging.
  System.Diagnostics.Debug.WriteLine("Error in Widget 42");
  // Use this example when tracing.
  System.Diagnostics.Trace.WriteLine("Error in Widget 42");

|| Operator (C# Reference

  // The following line displays True, because 42 is evenly  
  // divisible by 7.
  Console.WriteLine("Divisible returns {0}.", Divisible(42, 7));

  // The following line displays False, because 42 is not evenly 
  // divisible by 5.
  Console.WriteLine("Divisible returns {0}.", Divisible(42, 5));

  // The following line displays False when method Divisible  
  // uses ||, because you cannot divide by 0. 
  // If method Divisible uses | instead of ||, this line 
  // causes an exception.
  Console.WriteLine("Divisible returns {0}.", Divisible(42, 0));

WIKIPedia C Sharp (programming language)

  int foo = 42;         // Value type.
Community
  • 1
  • 1

6 Answers6

28

It's from The Hitch Hiker's Guide to the Galaxy.

In The Hitchhiker's Guide to the Galaxy (published in 1979), the characters visit the legendary planet Magrathea, home to the now-collapsed planet-building industry, and meet Slartibartfast, a planetary coastline designer who was responsible for the fjords of Norway. Through archival recordings, he relates the story of a race of hyper-intelligent pan-dimensional beings who built a computer named Deep Thought to calculate the Answer to the Ultimate Question of Life, the Universe, and Everything. When the answer was revealed to be 42, Deep Thought explained that the answer was incomprehensible because the beings didn't know what they were asking. It went on to predict that another computer, more powerful than itself would be made and designed by it to calculate the question for the answer. (Later on, referencing this, Adams would create the 42 Puzzle, a puzzle which could be approached in multiple ways, all yielding the answer 42.)

Andomar
  • 225,110
  • 44
  • 364
  • 390
16

The answer is, as people already have stated, The Hitchhiker's Guide to the Galaxy.

I made a little experiment and put a couple of numbers in the search field, and these are the results:

enter image description here

It seems like 42 beats its neighbors clearly, but it can't touch regular numbers like 40, 45 and 50, no matter how magical it is.

It would be interesting to do the same search in source code only.

Victor Sand
  • 2,259
  • 13
  • 32
9

Dude!

It's the Answer to the Ultimate Question of Life, the Universe, and Everything! As computed by Deep Thought supercomputer, which took 7.5 million years!

http://en.wikipedia.org/wiki/The_answer_to_life_the_universe_and_everything#Answer_to_the_Ultimate_Question_of_Life.2C_the_Universe_and_Everything_.2842.29

iluxa
  • 6,883
  • 17
  • 34
7

Check this out. 42 is the ultimate answer to the ultimate question of life the universe and everything

Martin Dvoracek
  • 1,670
  • 6
  • 26
  • 54
4

This is from The Hitch hikers Guide to the Galaxy and is:

The Answer to the Ultimate Question of Life, the Universe, and Everything

WikiLink

Belogix
  • 8,011
  • 1
  • 25
  • 32
4

Refer The Hitch Hiker's Guide to the Galaxy.

Rahul Bobhate
  • 4,678
  • 3
  • 24
  • 48