魔法数字42的起源是什么?编码中不可或缺? [英] What is the origin of magic number 42, indispensable in coding?

查看:181
本文介绍了魔法数字42的起源是什么?编码中不可或缺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

令人惊讶的是它被如此沉重的压缩...



问题是编码相关,在提出这个问题之前,我已经结合使用了42:




  • site :msdn.micrsoft.com

  • 代码示例

  • c#

  • < 魔法数


而我不是西方文化/文学的专家/粉丝。



还发现,为什么变量i和j用于计数器? [重复] 没有关闭,但是甚至受到保护。






我觉得大家都知道,除了我...



无处不在的魔法数字42的起源是什么用于代码示例和样本?



你如何使用42?因为我没有来过或曾经使用 42



经过一些搜索,我找到了MSDN文档:魔术数字:整数




  • 除了书/电影参考,开发人员经常将其用作任意值


$ b $嗯,这没有解释我什么。



所有这几年参与开发,编码和编程以及围绕IT相关活动(如rwquirements分析,系统管理等),哪些电影和书籍? ?



某些使用42(仅C#相关)代码片段的文本的引用:



JérômeLaban。 C#异步提示和技巧,第3部分:任务和同步上下文

  var t = Task.Delay(TimeSpan.FromSeconds(1))
.ContinueWith

_ => Task.Delay(TimeSpan.FromSeconds(42))
);

MSDN 异步代理库

  send(_target,42); 

快速入门:在C#或Visual Basic中调用异步API

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

使用PPL的C ++异步编程

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

Boxing and Unboxing(C#Programming Guide)

  Console.WriteLine(String.Con猫(答案,42,真)); 

如何:覆盖ToString方法(C#编程指南)

  int x = 42; 

跟踪听众

  /调试时使用这个例子。 
System.Diagnostics.Debug.WriteLine(Widget 42中的错误);
//跟踪时使用此示例。
System.Diagnostics.Trace.WriteLine(Widget 42中的错误);

||运算符(C#参考

  //以下行显示为True,因为42是均匀的
//可分为7.
Console.WriteLine(Divisible returns {0},Divisible(42,7));

//以下行显示False,因为42不是均匀的
//可分为5.
Console.WriteLine(Divisible returns {0},Divisible(42,5));

//以下行显示False当方法Divisible
//使用||,因为你不能除以0.
//如果方法Divisible使用|而不是||,这行
//导致异常
Console.WriteLine(Divisible return {0},Divisible(42,0));

WIKIPedia C Sharp(编程语言)

  int foo = 42; //值类型


解决方案

答案我正如人们已经说过的那样,银河的搭便车指南



我做了一些实验,并在搜索字段中输入了几个数字,结果如下:



< img src =https://i.stack.imgur.com/ydCHv.pngalt =enter image description here>



看起来像42邻居清楚,但不能像40,45和50的常规​​数字,无论多么神奇。



仅在源代码中进行相同的搜索将是有趣的。


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.

解决方案

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:

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.

这篇关于魔法数字42的起源是什么?编码中不可或缺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆