Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I view the code behind the .net library?

Tags:

c#

asp.net

vb.net

For instance, in my coding I using 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 produce my own.

Reason for the question:

I was recently asked in an interview to produce a reverse function that does not use the .Reverse function from the .net library. I nailed it, but there are other basic functions to learn such as formatting a number to a set amount of decimal places. Being able to look at the code behind the .net library functions would aide me in this pursuit.

TIA - Thanks in advance.

James

like image 984
James Cunningham Avatar asked Dec 27 '16 15:12

James Cunningham


People also ask

What is the code behind?

Code-behind is a technique used in Microsoft programming platforms, including ASP.NET and XAML, where the practice is facilitated by separating the visual and code aspects through the use of partial classes.

What type of code is found in code behind class?

Code Behind refers to the code for an ASP.NET Web page that is written in a separate class file that can have the extension of . aspx. cs or . aspx.

How is a ASP.NET presentation page associated with its code behind?

The code-behind feature of ASP.NET enables you to divide an ASP.NET page into two files - one consisting of the presentation data, and the second, which is also called the code-behind file, consisting of all the business logic.

Is .NET framework a library?

. NET has an expansive standard set of class libraries, referred to as either the base class libraries (core set) or framework class libraries (complete set). These libraries provide implementations for many general and app-specific types, algorithms, and utility functionality.


2 Answers

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

There was a free eBook published which tries to explain the inner workings of much of the SSCLI2, which is a slimmed down version of .Net 2.

InternalCall is handled by the runtime using mappings defined in the file ecalllist.h in coreclr, or in ecall.cpp for sscli. For example, Math.Round is mapped 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 then look at the class COMDouble, whose implementation we can find in comfloat.cpp, we see the code behind Math.Round:

/*====================================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
like image 141
Mitch Avatar answered Sep 30 '22 18:09

Mitch


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

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

like image 29
mm8 Avatar answered Sep 30 '22 18:09

mm8