Click here to Skip to main content
15,915,093 members
Home / Discussions / C#
   

C#

 
General[SQL] Using 'Like' operator for prepared statement Pin
ting66822-Dec-04 19:17
ting66822-Dec-04 19:17 
GeneralRe: [SQL] Using 'Like' operator for prepared statement Pin
Heath Stewart22-Dec-04 19:26
protectorHeath Stewart22-Dec-04 19:26 
GeneralDirectX and C# - Display AVI file Pin
Ahindika22-Dec-04 18:50
Ahindika22-Dec-04 18:50 
GeneralRe: DirectX and C# - Display AVI file Pin
Heath Stewart22-Dec-04 20:00
protectorHeath Stewart22-Dec-04 20:00 
GeneralDatagrid with textbox Pin
marck.santos22-Dec-04 15:15
marck.santos22-Dec-04 15:15 
GeneralRe: Datagrid with textbox Pin
Heath Stewart22-Dec-04 19:33
protectorHeath Stewart22-Dec-04 19:33 
GeneralPassing Value Types by Reference Pin
paulsawyer22-Dec-04 15:00
paulsawyer22-Dec-04 15:00 
GeneralRe: Passing Value Types by Reference Pin
Heath Stewart22-Dec-04 19:17
protectorHeath Stewart22-Dec-04 19:17 
This is called boxing and unboxing[^]. When you pass a struct by reference or as an object (including interfaces) an object is created on the heap to match the struct and the struct's data is copied to the object. Unboxing is when an instance of a struct is created and the object data is copied to its fields. This is a very expensive operations and is one of the major reason that generics were introduced into the CLI 2.0 and C# 2.0.

Ask yourself why you have a struct? Is it a remnant of your C/C++ experience? Forget what you know about pointers in C/C++. It hardly applies (it's still important when P/Invoking and with COM interop, but you seem to already have some idea since you know about pinning).

Structs in .NET are for short-lived, small data structures. Anything that has a lifespan and gets passed around quite a bit should be a class.

If you use a disassembler like ildasm.exe or a decompiler like .NET Reflector you can actually see that instead of structs Microsoft uses classes internally within the BCL quite a bit to avoid the boxing and unboxing. When the class is marshaled to managed code - and is attributed and written to align properly - native code sees it as a struct. A class is, after all, not much different than a struct in native code.

This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles] [My Blog]
GeneralRe: Passing Value Types by Reference Pin
Salil Khedkar22-Dec-04 19:20
Salil Khedkar22-Dec-04 19:20 
GeneralProblem with Stored Procedure Pin
ronin177022-Dec-04 14:29
ronin177022-Dec-04 14:29 
GeneralRe: Problem with Stored Procedure Pin
Javier Lozano22-Dec-04 18:36
Javier Lozano22-Dec-04 18:36 
Generalproblem auto increment field Pin
Gedrain22-Dec-04 13:30
Gedrain22-Dec-04 13:30 
GeneralRe: problem auto increment field Pin
Skynyrd22-Dec-04 13:35
Skynyrd22-Dec-04 13:35 
GeneralRe: problem auto increment field Pin
Heath Stewart22-Dec-04 19:07
protectorHeath Stewart22-Dec-04 19:07 
GeneralRe: problem auto increment field Pin
Gedrain23-Dec-04 3:13
Gedrain23-Dec-04 3:13 
GeneralRe: problem auto increment field Pin
Heath Stewart23-Dec-04 4:59
protectorHeath Stewart23-Dec-04 4:59 
GeneralTextBox Pin
Aviv Halperin22-Dec-04 11:56
Aviv Halperin22-Dec-04 11:56 
GeneralRe: TextBox Pin
Heath Stewart22-Dec-04 12:11
protectorHeath Stewart22-Dec-04 12:11 
GeneralRe: TextBox Pin
Aviv Halperin22-Dec-04 12:17
Aviv Halperin22-Dec-04 12:17 
GeneralRe: TextBox Pin
Heath Stewart22-Dec-04 12:40
protectorHeath Stewart22-Dec-04 12:40 
GeneralRe: TextBox Pin
Aviv Halperin22-Dec-04 12:20
Aviv Halperin22-Dec-04 12:20 
GeneralRe: TextBox Pin
Skynyrd22-Dec-04 12:29
Skynyrd22-Dec-04 12:29 
GeneralRe: TextBox Pin
Aviv Halperin22-Dec-04 12:39
Aviv Halperin22-Dec-04 12:39 
GeneralRe: TextBox Pin
Skynyrd22-Dec-04 12:46
Skynyrd22-Dec-04 12:46 
GeneralRe: TextBox Pin
Heath Stewart22-Dec-04 12:50
protectorHeath Stewart22-Dec-04 12:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.