请问c#和。net的区别是什么?当我想到c#的时候,我马上会说它是一种。net语言,但是当我找工作的时候,他们要求应聘者有c#和。net经验。谁能给我一个解释?


当前回答

除了Andrew的回答,值得注意的是:

. net不仅仅是一个库,还是一个用于执行应用程序的运行时。 对c#的了解意味着对。net的了解(因为c#对象模型对应于。net对象模型,您可以通过使用。net库在c#中做一些有趣的事情)。相反的情况并不一定正确,因为您可以使用其他语言来编写. net应用程序。

在。net / c#中,语言、运行库和库之间的区别比在c++中更为严格,c++的语言规范还包括一些基本的库函数。c#规范只说了很少的环境(基本上,它应该包含一些类型,如int,但这或多或少是全部)。

其他回答

c#是一种语言,. net是一个应用程序框架。. net库可以在CLR上运行,因此任何可以在CLR上运行的语言也可以使用。net库。

如果你熟悉Java,这是类似的……Java是一种构建在JVM之上的语言…尽管任何预先组装的Java库都可以被构建在JVM之上的另一种语言使用。

在。net中,你不仅可以找到c#。比如你可以找到Visual Basic。如果一份工作需要。net知识,那么它可能需要一个了解。net框架所提供的全部语言集的程序员。

[C#]

是一种有规则的语言,你可以如何写代码,变量,类等,这只是c#零。net: SumTwo(int a, int b){ 返回a + b; }

(。净)

是一个在环境中运行程序的框架,提供了可以跨不同操作系统共享的广泛资源列表。 看这个例子,其中有一个。net方法: File.AppendAllText("file.txt", "DEMO CONTENT");

在这个例子中,c#代码将调用。net来创建一个文件并添加一个内容,在Linux和Windows上的执行将是不同的,但是。net会处理这些差异,让你使用相同的契约,而不必担心在c#级别上的差异。

除了Andrew的回答,值得注意的是:

. net不仅仅是一个库,还是一个用于执行应用程序的运行时。 对c#的了解意味着对。net的了解(因为c#对象模型对应于。net对象模型,您可以通过使用。net库在c#中做一些有趣的事情)。相反的情况并不一定正确,因为您可以使用其他语言来编写. net应用程序。

在。net / c#中,语言、运行库和库之间的区别比在c++中更为严格,c++的语言规范还包括一些基本的库函数。c#规范只说了很少的环境(基本上,它应该包含一些类型,如int,但这或多或少是全部)。

当人们谈论“。net框架”时,他们倾向于将两个主要领域结合起来——运行时库和实际运行。net代码的虚拟机。

When you create a class library in Visual Studio in C#, the DLL follows a prescribed format - very roughly, there is section that contains meta data that describes what classes are included in it and what functions they have, etc.. and that describes where in the binary those objects exist. This common .net format is what allows libraries to be shared between .net languages (C#, VB.Net, F# and others) easily. Although much of the .net "runtime library" is written in C# now (I believe), you can imagine how many of them could have been written in unmanaged languages but arranged in this prescribed format so that they could be consumed by .net languages.

你所构建的库的真正“肉”是由CIL(“公共中间语言”)组成的,它有点像。net的汇编语言——同样,这种语言是所有。net语言的公共输出,这使得。net库可以被任何。net语言使用。

使用“ildasm.exe”工具,它在微软的sdk中是免费的(可能已经在你的电脑上了),你可以看到c#代码是如何转换成元数据和IL的。我在这个答案的底部包含了一个示例作为示例。

当您运行执行。net代码时,通常发生的情况是。net虚拟机正在读取并处理该IL。这是.net的另一方面,同样,您可以想象这可以很容易地用非托管语言编写——它“只”需要读取VM指令并运行它们(并与垃圾收集器集成,这也不需要是.net代码)。

我所描述的是(再次粗略地)在Visual Studio中构建可执行文件时会发生什么(要了解更多信息,我强烈推荐Jeffrey Richter的《CLR via c#》这本书——它非常详细,写得非常好)。

However, there are times that you might write C# that will not be executed within a .net environment - for example, Bridge.NET "compiles" C# code into JavaScript which is then run in the browser (the team that produce it have gone to the effort of writing versions of the .net runtime library that are written in JavaScript and so the power and flexibility of the .net library is available to the generated JavaScript). This is a perfect example of the separation between C# and .net - it's possible to write C# for different "targets"; you might target the .net runtime environment (when you build an executable) or you might target the browser environment (when you use Bridge.NET).

一个(非常)简单的例子类:

using System;

namespace Example
{
    public class Class1
    {
        public void SayHello()
        {
            Console.WriteLine("Hello");
        }
    }
}

生成的元数据和IL(通过ildasm.exe检索):

// Metadata version: v4.0.30319
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly Example
{
  .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
                                                                                                            63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.

  // --- The following custom attribute is added automatically, do not uncomment -------
  //  .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 07 01 00 00 00 00 ) 

  .custom instance void [mscorlib]System.Reflection.AssemblyTitleAttribute::.ctor(string) = ( 01 00 0A 54 65 73 74 49 4C 44 41 53 4D 00 00 )    // ...TestILDASM..
  .custom instance void [mscorlib]System.Reflection.AssemblyDescriptionAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyConfigurationAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyCompanyAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyProductAttribute::.ctor(string) = ( 01 00 0A 54 65 73 74 49 4C 44 41 53 4D 00 00 )    // ...TestILDASM..
  .custom instance void [mscorlib]System.Reflection.AssemblyCopyrightAttribute::.ctor(string) = ( 01 00 12 43 6F 70 79 72 69 67 68 74 20 C2 A9 20   // ...Copyright .. 
                                                                                                  20 32 30 31 36 00 00 )                            //  2016..
  .custom instance void [mscorlib]System.Reflection.AssemblyTrademarkAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Runtime.InteropServices.ComVisibleAttribute::.ctor(bool) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Runtime.InteropServices.GuidAttribute::.ctor(string) = ( 01 00 24 31 39 33 32 61 32 30 65 2D 61 37 36 64   // ..$1932a20e-a76d
                                                                                                  2D 34 36 33 35 2D 62 36 38 66 2D 36 63 35 66 36   // -4635-b68f-6c5f6
                                                                                                  32 36 36 31 36 37 62 00 00 )                      // 266167b..
  .custom instance void [mscorlib]System.Reflection.AssemblyFileVersionAttribute::.ctor(string) = ( 01 00 07 31 2E 30 2E 30 2E 30 00 00 )             // ...1.0.0.0..
  .custom instance void [mscorlib]System.Runtime.Versioning.TargetFrameworkAttribute::.ctor(string) = ( 01 00 1C 2E 4E 45 54 46 72 61 6D 65 77 6F 72 6B   // ....NETFramework
                                                                                                        2C 56 65 72 73 69 6F 6E 3D 76 34 2E 35 2E 32 01   // ,Version=v4.5.2.
                                                                                                        00 54 0E 14 46 72 61 6D 65 77 6F 72 6B 44 69 73   // .T..FrameworkDis
                                                                                                        70 6C 61 79 4E 61 6D 65 14 2E 4E 45 54 20 46 72   // playName..NET Fr
                                                                                                        61 6D 65 77 6F 72 6B 20 34 2E 35 2E 32 )          // amework 4.5.2
  .hash algorithm 0x00008004
  .ver 1:0:0:0
}
.module Example.dll
// MVID: {80A91E4C-0994-4773-9B73-2C4977BB1F17}
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003       // WINDOWS_CUI
.corflags 0x00000001    //  ILONLY
// Image base: 0x05DB0000

// =============== CLASS MEMBERS DECLARATION ===================

.class public auto ansi beforefieldinit Example.Class1
       extends [mscorlib]System.Object
{
  .method public hidebysig instance void 
          SayHello() cil managed
  {
    // Code size       13 (0xd)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ldstr      "Hello"
    IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
    IL_000b:  nop
    IL_000c:  ret
  } // end of method Class1::SayHello

  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
    // Code size       8 (0x8)
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  nop
    IL_0007:  ret
  } // end of method Class1::.ctor

} // end of class Example.Class1

// =============================================================