Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3228468
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:41:46+00:00 2026-05-17T16:41:46+00:00

Assume I have a class library project name Utilities.dll as follows: public static class

  • 0

Assume I have a class library project name Utilities.dll as follows:

public static class Utilities
{
    public static T Min<T>(T[] data) where T : IComparable<T>
    {
        T min = data[0];
        foreach (T x in data)
        {
            if (x.CompareTo(min) < 0)
                min = x;
        }
        return min;
    }
}

Then I create a new console app project Tester.exe referencing Utilities.dll as follows

class Program
{
    static void Main(string[] args)
    {
        int[] data = { 3, 2, 4, 5, 1, 0, -1, 10 };

        Console.WriteLine(Utilities.Min<int>(data));
    }
}

I want to know how the compiler does its job behind the scene. Can anybody know the rough idea about it?

Note that: Assume I have no access to the source code of Utilities.dll when using it on Tester.exe project. Only Utilities.dll is provided.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:41:47+00:00Added an answer on May 17, 2026 at 4:41 pm

    In .NET (and unlike things like C++), it is the runtime that provides generics. The IL is structured to retain all the generic information (contrasting to Java, which uses type erasure).

    In fact, you can create a new Type on the fly (via TypeBuilder), and use reflecton:

    Type someCrazyNewType = InventType(...); // not shown
    Type listType = typeof(List<>).MakeGenericType(someCrazyNewType);
    IList list = (IList)Activator.CreateInstance(listType);
    

    OT, but re your specific example, I actually recommend a change:

    var comparer = Comparer<T>.Default;
    foreach (T x in data)
    {
        if (comparer.Compare(x, min) < 0)
            min = x;
    }
    

    This will work even if x is null, and supports “lifted” comparisons (against Nullable<T>). You can also then remove the generic constraint.

    But if we look at your original function, the IL is:

    .method public hidebysig static !!T Min<([mscorlib]System.IComparable`1<!!T>) T>(!!T[] data) cil managed
    {
        .maxstack 2
        .locals init (
            [0] !!T min,
            [1] !!T x,
            [2] !!T[] CS$6$0000,
            [3] int32 CS$7$0001)
        L_0000: ldarg.0 
        L_0001: ldc.i4.0 
        L_0002: ldelem.any !!T
        L_0007: stloc.0 
        L_0008: ldarg.0 
        L_0009: stloc.2 
        L_000a: ldc.i4.0 
        L_000b: stloc.3 
        L_000c: br.s L_002d
        L_000e: ldloc.2 
        L_000f: ldloc.3 
        L_0010: ldelem.any !!T
        L_0015: stloc.1 
        L_0016: ldloca.s x
        L_0018: ldloc.0 
        L_0019: constrained !!T
        L_001f: callvirt instance int32 [mscorlib]System.IComparable`1<!!T>::CompareTo(!0)
        L_0024: ldc.i4.0 
        L_0025: bge.s L_0029
        L_0027: ldloc.1 
        L_0028: stloc.0 
        L_0029: ldloc.3 
        L_002a: ldc.i4.1 
        L_002b: add 
        L_002c: stloc.3 
        L_002d: ldloc.3 
        L_002e: ldloc.2 
        L_002f: ldlen 
        L_0030: conv.i4 
        L_0031: blt.s L_000e
        L_0033: ldloc.0 
        L_0034: ret 
    }
    

    You can see the T (for generics) is there, in full. The JIT (in most CLI implementations, not all) is responsible for figuring out what is necessary; typically all reference-type implementations share an implementation (since all references look the same under the hood), but each value-type implementation gets a separate JIT (as they have different memory layouts).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have some class in my project. Now I create another Class library
Assume I have a class that looks like this: class Sample { public string
Assume I have a class class A { char *attr1,*attr2; public: . . .
Lets assume we have a class car. How would You name parameters of function
Assume I have a C# class like this: [XmlRoot(floors)] public class FloorCollection { [XmlElement(floor)]
Assume I have a .Net library with a class(TestClass) with a function 'Add'. I
I have a C++/CLI class library project in VS2005 that i am having some
When we have an exe or dll and a static library attached to it,
I have a static class in my Class Library called Lookup, I am using
Assume that I have the following object public class MyClass { public ReadOnlyDictionary<T, V>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.