简体   繁体   English

C# 和 .NET 有什么区别?

[英]What is the difference between C# and .NET?

May I know what is the difference between C# and .NET?我可以知道 C# 和 .NET 有什么区别吗? When I think of C#, right away I would say it is a .NET language, but when I search for job posts, they require candidates to have C# and .NET experience.当我想到 C# 时,我马上会说它是 .NET 语言,但是当我搜索职位时,他们要求候选人有 C#0972AZ19BB0582EDB 经验。 Can someone give me an explanation?有人可以给我一个解释吗?

C# is a programming language, .NET is a blanket term that tends to cover both the .NET Framework (an application framework library) and the Common Language Runtime which is the runtime in which .NET assemblies are run. C# 是一种编程语言,.NET 是一个笼统的术语,倾向于涵盖 .NET Framework(应用程序框架库)和公共语言运行时,即运行 .NET 程序集的运行时。

Microsoft's implementation of C# is heavily integrated with the .NET Framework so it is understandable that the two concepts would be confused. Microsoft 的 C# 实现与 .NET Framework 高度集成,因此这两个概念会混淆是可以理解的。 However it is important to understand that they are two very different things.但是,重要的是要了解它们是两个非常不同的事物。

Here is a class written in C#:这是一个用 C# 编写的类:

class Example { }

Here is a class written in C# that explicitly uses a .NET framework assembly, type, and method:这是一个用 C# 编写的类,它显式使用 .NET 框架程序集、类型和方法:

class Example
{
    static void Main()
    {
        // Here we call into the .NET framework to 
        // write to the output console
        System.Console.Write("hello, world");
    }
}

As I mentioned before, it is very difficult to use Microsoft's implementation of C# without using the .NET framework as well.正如我之前提到的,如果不使用 .NET 框架,很难使用 Microsoft 的 C# 实现。 My first Example implementation above even uses the .NET framework (implicitly, yes, but it does use it nonetheless) because Example inherits from System.Object .我上面的第一个Example实现甚至使用了 .NET 框架(隐含地,是的,但它确实使用它)因为Example继承自System.Object

Also, the reason I use the phrase Microsoft's implementation of C# is because there are other implementations of C# available .另外,我使用Microsoft's implementation of C#这个短语的原因是因为还有其他可用的 C# 实现

In addition to Andrew's answer , it is worth noting that:除了安德鲁的回答,值得注意的是:

  • .NET isn't just a library , but also a runtime for executing applications. .NET 不仅是一个,而且还是一个用于执行应用程序的运行时
  • The knowledge of C# implies some knowledge of .NET (because the C# object model corresponds to the .NET object model and you can do something interesting in C# just by using .NET libraries). C# 的知识意味着对 .NET 的一些了解(因为 C# 对象模型对应于 .NET 对象模型,您可以通过使用 .NET 库在 C# 中做一些有趣的事情)。 The opposite isn't necessarily true as you can use other languages to write .NET applications.反之亦然,因为您可以使用其他语言编写 .NET 应用程序。

The distinction between a language , a runtime , and a library is more strict in .NET/C# than for example in C++, where the language specification also includes some basic library functions.在 .NET/C# 中,语言运行时之间的区别比在 C++ 中更为严格,其中语言规范还包括一些基本的库函数。 The C# specification says only a very little about the environment (basically, that it should contain some types such as int , but that's more or less all). C# 规范对环境只说了很少的内容(基本上,它应该包含一些类型,例如int ,但这或多或少是全部)。

C# 是一种编程语言,.NET 是构建该语言的框架。

C# is a strong Object Oriented programming language that is mostly built on the .NET framework. C# 是一种强大的面向对象编程语言,主要构建在 .NET 框架上。

C# is the airplane and .NET is the runway ;) C# 是飞机,.NET 是跑道;)

C# is a language, .NET is an application framework. C# 是一种语言,.NET 是一种应用程序框架。 The .NET libraries can run on the CLR and thus any language which can run on the CLR can also use the .NET libraries. .NET 库可以在 CLR 上运行,因此任何可以在 CLR 上运行的语言也可以使用 .NET 库。

If you are familiar with Java, this is similar... Java is a language built on top of the JVM... though any of the pre-assembled Java libraries can be used by another language built on top of the JVM.如果您熟悉 Java,则类似... Java 是一种构建在 JVM 之上的语言...尽管任何预组装的 Java 库都可以被构建在 JVM 之上的另一种语言使用。

When people talk about the ".net framework" they tend to be combining two main areas - the runtime library and the virtual machine that actually runs the .net code.当人们谈论“.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.当您在 C# 中的 Visual Studio 中创建类库时,DLL 遵循规定的格式 - 非常粗略地说,有一个部分包含描述其中包含哪些类以及它们具有哪些功能等的元数据,并且描述了在哪里在二进制文件中,这些对象存在。 This common .net format is what allows libraries to be shared between .net languages (C#, VB.Net, F# and others) easily.这种常见的 .net 格式允许在 .net 语言(C#、VB.Net、F# 等)之间轻松共享库。 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.尽管现在大部分 .net “运行时库”都是用 C# 编写的(我相信),但您可以想象其中有多少是用非托管语言编写的,但以这种规定的格式排列,以便它们可以被 .net 语言使用.

The real "meat" of the library that you build consists of CIL ("Common Intermediate Language") which is a bit like the assembly language of .net - again, this language is the common output of all .net languages, which is what makes .net libraries consumable by any .net language.您构建的库的真正“肉”由 CIL(“通用中间语言”)组成,它有点像 .net 的汇编语言 - 同样,这种语言是所有 .net 语言的通用输出,这就是使任何 .net 语言都可以使用 .net 库。

Using the tool "ildasm.exe", which is freely available in Microsoft SDKs (and might already be on your computer), you can see how C# code is converted into meta data and IL.使用 Microsoft SDK 中免费提供的工具“ildasm.exe”(并且可能已经在您的计算机上),您可以看到 C# 代码如何转换为元数据和 IL。 I've included a sample at the bottom of this answer as an example.作为示例,我在此答案的底部包含了一个示例。

When you run execute .net code, what is commonly happening is the .net virtual machine is reading that IL and processing it.当您运行执行 .net 代码时,通常发生的是 .net 虚拟机正在读取该 IL 并对其进行处理。 This is the other side of .net and, again, you can likely imagine that this could easily be written in an unmanaged language - it "only" needs to read VM instructions and run them (and integrate with the garbage collector, which also need not be .net code).这是 .net 的另一面,同样,您可以想象这可以很容易地用非托管语言编写——它“只”需要读取 VM 指令并运行它们(并与垃圾收集器集成,垃圾收集器也需要不是 .net 代码)。

What I've described is (again, roughly) what happens when you build an executable in Visual Studio (for more information, I highly recommend the book "CLR via C# by Jeffrey Richter" - it's very detailed and excellently written).我所描述的是(再次,粗略地)在 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).但是,有时您可能会编写不会在 .net 环境中执行的 C# - 例如, Bridge.NET将 C# 代码“编译”成 JavaScript,然后在浏览器中运行(产生它的团队已经去了编写用 JavaScript 编写的 .net 运行时库版本的工作,因此 .net 库的功能和灵活性可用于生成的 JavaScript)。 This is a perfect example of the separation between C# and .net - it's possible to write C# for different "targets";这是 C# 和 .net 之间分离的完美示例 - 可以为不同的“目标”编写 C#; you might target the .net runtime environment (when you build an executable) or you might target the browser environment (when you use Bridge.NET).您可以针对 .net 运行时环境(当您构建可执行文件时),也可以针对浏览器环境(当您使用 Bridge.NET 时)。

A (very) simple example class:一个(非常)简单的示例类:

using System;

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

The resulting meta data and IL (retrieved via ildasm.exe):生成的元数据和 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

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

In .NET you don't find only C#.在 .NET 中,您不仅可以找到 C#。 You can find Visual Basic for example.例如,您可以找到 Visual Basic。 If a job requires .NET knowledge, probably it need a programmer who knows the entire set of languages provided by the .NET framework.如果一项工作需要.NET 知识,那么它可能需要一个了解.NET 框架提供的整套语言的程序员。

C# is a programming language. C# 是一种编程语言。

.Net is a framework used for building applications on Windows. .Net 是用于在 Windows 上构建应用程序的框架。

.Net framework is not limited to C#. .Net 框架不限于 C#。 Different languages can target .Net framework and build applications using that framework.不同的语言可以针对 .Net 框架并使用该框架构建应用程序。 Examples are F# or VB.Net示例是 F# 或 VB.Net

C# does not have a seperate runtime library. C#没有单独的运行时库。 It uses .NET as a runtime library.它使用.NET作为运行时库。

Here I provided you a link where explain what is C# Language and the .NET Framework Platform Architecture .在这里,我为您提供了一个链接,其中解释了什么是C# 语言.NET Framework 平台架构 Remember that C# is a general-purpose, object-oriented programming language, and it runs on the .NET Framework.请记住,C# 是一种通用的、面向对象的编程语言,它在 .NET Framework 上运行。

.NET Framework includes a large class library named Framework Class Library (FCL) and provides user interface, data access, database connectivity, cryptography, web application development, numeric algorithms, and network communications. .NET Framework 包括一个名为 Framework Class Library (FCL) 的大型类库,并提供用户界面、数据访问、数据库连接、密码学、Web 应用程序开发、数字算法和网络通信。

.NET Framework was developed by Microsoft that runs primarily on Microsoft Windows. .NET Framework 由 Microsoft 开发,主要在 Microsoft Windows 上运行。

Introduction to the C# Language and the .NET Framework from Microsoft Docs Microsoft Docs 中的 C# 语言和 .NET Framework 简介

[C#] [C#]

  • Is a language with rules on how you can write code, variables, classes etc, this is only C# with zero .net:是一种对如何编写代码、变量、类等有规则的语言,这只是 C# 零 .net:

     public int SumTwo(int a, int b){ return a + b; }

[.NET] [。网]

  • Is a framework to run programs into an environment, provides a wide list of resources that might be shared across different OS.是一个在环境中运行程序的框架,提供了可能在不同操作系统之间共享的大量资源。

  • See this example with one .NET method:使用一种 .NET 方法查看此示例:

     File.AppendAllText("file.txt", "DEMO CONTENT");

In this example, the C# code will be calling .NET to create a file and add a content, the execution will be different on Linux and Windows but the .NET will take care of these differences letting you use the same contract and don't having to worry about that on C# level. In this example, the C# code will be calling .NET to create a file and add a content, the execution will be different on Linux and Windows but the .NET will take care of these differences letting you use the same contract and don't having to担心 C# 级别的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM