Where can I view the .net library code?

For example, in my coding I use the VB function

Round(1.325, 2) 

Which produces the number 1.33. I am wondering if there is a place to view the code behind the Round function so that I can create my own.

Reason for question:

Recently, I was asked to create a reverse function that does not use the .Reverse function from the .net library. I nailed it, but there are other basic functions, such as formatting a number to a given number of decimal places. Being able to take a look at the code underlying the .net library functions will help me in this pursuit.

TIA - Thanks in advance.

James

+5
source share
2 answers

For parts of the .NET Framework that are implemented in C #, it is easiest to view them using http://referencesource.microsoft.com/ . For the rest, you either need to download a copy of SSCLI, or browse the coreclr repository on GitHub.

A free e-book has been published that attempts to explain the inner workings of most of SSCLI2, which is a smaller version of .NET 2.

InternalCall handled by the runtime using the mappings defined in the ecalllist.h file in coreclr or in ecall.cpp for sscli, For example, Math.Round is displayed as:

 // snip FCFuncStart(gMathFuncs) FCIntrinsic("Sin", COMDouble::Sin, CORINFO_INTRINSIC_Sin) FCIntrinsic("Cos", COMDouble::Cos, CORINFO_INTRINSIC_Cos) FCIntrinsic("Sqrt", COMDouble::Sqrt, CORINFO_INTRINSIC_Sqrt) FCIntrinsic("Round", COMDouble::Round, CORINFO_INTRINSIC_Round) // snip 

If we look at the COMDouble class, the implementation of which we can find in comfloat.cpp , we see the Math.Round code:

 /*====================================Round===================================== ** ==============================================================================*/ FCIMPL1_V(double, COMDouble::Round, double x) FCALL_CONTRACT; // If the number has no fractional part do nothing // This shortcut is necessary to workaround precision loss in borderline cases on some platforms if (x == (double)((INT64)x)) { return x; } // We had a number that was equally close to 2 integers. // We need to return the even one. double tempVal = (x + 0.5); double flrTempVal = floor(tempVal); if ((flrTempVal == tempVal) && (fmod(tempVal, 2.0) != 0)) { flrTempVal -= 1.0; } return _copysign(flrTempVal, x); FCIMPLEND 
+5
source

As mentioned in the comments, you can download and view the .NET Framework source code at https://referencesource.microsoft.com/ .

Note that some managed methods are just wrappers for native methods. You can find an implementation of some of them that are part of .NET Core on GitHub: https://github.com/dotnet/coreclr/tree/32f0f9721afb584b4a14d69135bea7ddc129f755

+2
source

Source: https://habr.com/ru/post/1261949/


All Articles