我可以知道C#和.NET有什么区别?当我想到C#时,我会说它是一种.NET语言,但是当我搜索职位时,他们要求候选人具有C#和.NET经验.有人可以给我一个解释吗?
And*_*are 184
C#是一种编程语言,.NET是一个笼统的术语,往往涵盖.NET Framework(应用程序框架库)和公共语言运行时,它是运行.NET程序集的运行时.
Microsoft的C#实现与.NET Framework高度集成,因此可以理解这两个概念会混淆.然而,重要的是要理解它们是两个非常不同的东西.
这是一个用C#编写的类:
class Example { }
Run Code Online (Sandbox Code Playgroud)
这是一个用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");
}
}
Run Code Online (Sandbox Code Playgroud)
正如我之前提到的,在不使用.NET框架的情况下使用Microsoft的C#实现非常困难.我Example上面的第一个实现甚至使用.NET框架(隐含地,是的,但它确实使用它),因为Example继承自System.Object.
此外,我使用短语Microsoft的C#实现的原因是因为还有其他C#实现可用.
Tom*_*cek 113
除了安德鲁所说的,值得注意的是:
在.NET/C#中,语言,运行时和库之间的区别比在C++中更严格,在C++中,语言规范还包括一些基本的库函数.C#规范对环境的描述很少(基本上,它应该包含一些类型,例如int,但这或多或少都是).
Pol*_*878 14
C#是一种语言,.NET是一种应用程序框架..NET库可以在CLR上运行,因此可以在CLR上运行的任何语言也可以使用.NET库.
如果你熟悉Java,那就类似了...... Java是一种构建在JVM之上的语言......虽然任何预组装的Java库都可以被构建在JVM之上的另一种语言使用.
当人们谈论".net框架"时,他们倾向于将两个主要领域 - 运行时库和实际运行.net代码的虚拟机结合起来.
当您在C#中使用Visual Studio创建类库时,DLL遵循规定的格式 - 非常粗略地,有一个部分包含元数据,该元数据描述了包含哪些类以及它们具有哪些功能等等.这描述了在二进制文件中存在这些对象.这种常见的.net格式允许在.net语言(C#,VB.Net,F#和其他语言)之间轻松共享库.尽管.net"运行时库"的大部分都是用C#编写的(我相信),你可以想象它们中有多少可以用非托管语言编写,但是以这种规定的格式排列,以便它们可以被.net语言使用.
您构建的库的真正"肉"由CIL("通用中间语言")组成,它有点像.net的汇编语言 - 再次,这种语言是所有.net语言的通用输出,这是什么使.net库可以被任何.net语言使用.
使用Microsoft SDK中免费提供的工具"ildasm.exe"(可能已经在您的计算机上),您可以看到C#代码如何转换为元数据和IL.我在这个答案的底部包含了一个示例作为示例.
当您运行执行.net代码时,通常发生的是.net虚拟机正在读取IL并对其进行处理.这是.net的另一面,再次,您可以想象这可以很容易地用非托管语言编写 - 它"只"需要读取VM指令并运行它们(并与垃圾收集器集成,这也需要不是.net代码).
我所描述的(再次,粗略地)当您在Visual Studio中构建可执行文件时会发生什么(更多信息,我强烈推荐"由Jeffrey Richter通过C#CLR"这本书 - 它非常详细且写得非常好).
但是,有时您可能会编写不会在.net环境中执行的C# - 例如,Bridge.NET将C#代码"编译"成JavaScript,然后在浏览器中运行(生成它的团队已经转到编写是用JavaScript编写的,因此动力和.NET库的灵活性.NET运行库版本的努力是提供给生成的JavaScript).这是C#和.net之间分离的完美例子 - 可以为不同的"目标"编写C#; 您可能会以.net运行时环境为目标(在构建可执行文件时),也可能以浏览器环境为目标(当您使用Bridge.NET时).
一个(非常)简单的示例类:
using System;
namespace Example
{
public class Class1
{
public void SayHello()
{
Console.WriteLine("Hello");
}
}
}
Run Code Online (Sandbox Code Playgroud)
生成的元数据和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
// =============================================================
Run Code Online (Sandbox Code Playgroud)
C#是一种编程语言。
.Net是用于在Windows上构建应用程序的框架。
.Net框架不限于C#。不同的语言可以针对.Net框架并使用该框架构建应用程序。例如F#或VB.Net
| 归档时间: |
|
| 查看次数: |
291825 次 |
| 最近记录: |