473,651 Members | 2,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic Type Instantiation

I don't understand the third paragraph under the heading 'Generic type
instantiations' on
<http://msdn.microsoft. com/vcsharp/2005/overview/language/generics/>:

| The .NET Common Language Runtime creates a specialized copy of the
| native code for each generic type instantiation with a value type,
| but shares a single copy of the native code for all reference
| types (since, at the native code level, references are just
| pointers with the same representation) .

Would someone mind putting that into different words?
Nov 17 '05 #1
4 2099
"Cool Guy" <co*****@abc.xy z> wrote in message
news:15******** ********@cool.g uy.abc.xyz...
I don't understand the third paragraph under the heading 'Generic type
instantiations' on
<http://msdn.microsoft. com/vcsharp/2005/overview/language/generics/>:

| The .NET Common Language Runtime creates a specialized copy of the
| native code for each generic type instantiation with a value type,
| but shares a single copy of the native code for all reference
| types (since, at the native code level, references are just
| pointers with the same representation) .

Would someone mind putting that into different words?


There is a single copy of the IL for a generic type. However, when the JIT
compiler has to emit the native code for any particular spcialization of the
generic type there are issues. For reference types its easy, the native code
is just dereferencing a pointer to the type used to specialise the generic
so they can all share the same native code.

However for value types things are more complex. Variables of the
specializing type will be different sizes and so a generic instance will
occupy different amounts of space. What about floating point and integer
arithmatic, the machine instructions will be different for these. So for
value types the JIT compiler emits a different copy of the native code for
each specialization.

class Foo<T>
{
}

Foo<string> and Foo<SqlConnecti on> can share the same native code

Foo<int>, Foo<long> and Foo<float> each have their own copy

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Nov 17 '05 #2
"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> wrote:
[...]

class Foo<T>
{
}

Foo<string> and Foo<SqlConnecti on> can share the same native code

Foo<int>, Foo<long> and Foo<float> each have their own copy


How about:

class Foo<T,U>
{
}

I assume that in this case Foo<string,Stri ngBuilder> and
Foo<Stream,Stre amWriter> would share the same native code, too. As would
Foo<string,int> and Foo<StringBuild er,int>. Right?
Nov 17 '05 #3
"Cool Guy" <co*****@abc.xy z> wrote in message
news:1q******** ******@cool.guy .abc.xyz...
"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> wrote:
[...]

class Foo<T>
{
}

Foo<string> and Foo<SqlConnecti on> can share the same native code

Foo<int>, Foo<long> and Foo<float> each have their own copy


How about:

class Foo<T,U>
{
}

I assume that in this case Foo<string,Stri ngBuilder> and
Foo<Stream,Stre amWriter> would share the same native code, too. As would
Foo<string,int> and Foo<StringBuild er,int>. Right?


Thats my understanding, yes

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Nov 17 '05 #4
"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> wrote:
Thats my understanding, yes


Thanks. :)
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
25761
by: Joanna Carter \(TeamB\) | last post by:
I want to be able to use a generic type, bind its generic parameters to specific type(s) and then hold newly created instances in a list of generic ypes. Sort of like this : class Agent<T> { ... }
8
1624
by: Cool Guy | last post by:
From <http://msdn2.microsoft.com/en-us/library/f4a6ta2h(en-US,VS.80).aspx>: | As with the previous use of the Stack<T> class created with the Order | type, another instance of the specialized Stack<T> class is created, | and the pointers contained therein are set to reference an area of | memory the size of a Customer type. I don't quite get this. Where in memory do these changes (the pointers getting set) take place?
6
4640
by: Dan Holmes | last post by:
I have a class that i need a constraint of int, string, float or bool. I have tried the following but can't make VS accept it. I read the docs and they showed that any value type can be used unless it is nullable. Why doesn't this work? public class Metadata<T> where T: System.int line from the docs
3
2187
by: Boris | last post by:
I have a class which should like this ideally: generic <typename T> public ref class ManagedClass { T ^managedMember; UnmanagedClass<U*unmanagedMember; }; I actually would like to specify the type U depending on the type T. As
7
2036
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation: displayMsgDelegate = DisplayStatusMessage; implementation: public void DisplayStatusMessage(string statusMessage)
3
2151
by: Peter K | last post by:
Hi I have an interface for a "factory" defined like this: public interface IDeviceFactory<T: IDisposable { T GetDevice(); }
11
5001
by: paul.gibson | last post by:
A simple code example is easier than trying to describe the issue. I have: public class myClassA {
11
2540
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use generics instead of System.Object. I want the code in Form1_Load to remain exactly the same, but in the background I want to use generics. I'm trying to get a better understanding of how it works and I'm a little stuck.
8
4373
by: Advait Mohan Raut | last post by:
Hello, I am using VC# 2005 ; C# 2.0 I am working on the performance measurement tool. For that I need my code to call user defined method along with its parameters. For that should I use a generic delegate with variable length argument or something else ? if it is then how to use it ? AA advait
0
8349
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8795
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8695
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8460
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7296
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.