|
if you consider using anything else but value types , then use a class. If not, you're free to use a struct
|
|
|
|
|
Jan Sommer wrote: if you consider using anything else but value types
Why should this make any difference? Can you explain that?
The struct/class may live on the stack/heap. In any case, it will contain its fields, so they will consequently live on the stack/heap as well. It is therefore absolutely irrelevant if they were values or refs.
Regards
Thomas
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|
Boxing occurs if you use reference types in structs.. also, if he only plans to use value types it doesn't allocate as much memory then classes... see http://msdn.microsoft.com/en-us/library/ah19swz4(VS.71).aspx for details
|
|
|
|
|
Jan Sommer wrote: Boxing occurs if you use reference types in structs..
Ummm, what? No! This would mean that whole graphs of classes would be copied from the heap to the stack eventuallly when referenced by a struct .
See here: http://en.csharp-online.net/Classes,_Structs,_and_Objects—When_Boxing_Occurs[^]
Jan Sommer wrote: also, if he only plans to use value types it doesn't allocate as much memory then classes
This is also wrong as soon as there is more than one reference to the class/struct.
Regards
Thomas
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|
Another minor difference: A struct is accessed by a pointer to its start in memory, but a class is accessed by a pointer to a pointer to its start. So if you're repeatedly accessing this object and you want the maximum efficiency, a struct will save one memory read per access.
As was pointed out earlier, a struct is less efficient to pass as a parameter because the whole object is pushed onto the stack. A class only has its reference (an address) put on the stack, so is faster to pass.
|
|
|
|
|
Alan Balkany wrote: As was pointed out earlier, a struct is less efficient to pass as a parameter because the whole object is pushed onto the stack. A class only has its reference (an address) put on the stack, so is faster to pass.
If the struct is small enough, it can be copied with a single instruction, making it just as efficient as copying a reference. It's recommended that a struct should not be larger than 16 bytes, so that it can be copied with a few instructions, without using a loop.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Unless you have a special reason to make a struct, make it a class.
Classes are easy to implement, just put a few properties in it and it works without problems. A struct needs more work and more knowledge to be implemented properly, or you may end up with a struct that does unexpected things.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
If you think of it as a value, make it a struct.
If having operators for it makes sense, you're likely thinking of it as a value.
|
|
|
|
|
I have a form with 3 tab pages. On the first tab I have text and drop down boxes. Some boxes you insert data, some are bound to database tables that you pick items and customers from. I want to save all this info to a separate table in the same database as a new entry. My problem is I don't know how. If I was to use a datagrid and just change the info and save it back to the same table that I can do. But to move it all to a different table thats the tickler. Any help would be great.
Thanks Don
|
|
|
|
|
1) Make a stored procedure in the database to insert the row(s)
2) Make a SqlCommand object in code with CommandText set to the name of the stored procedure
3) Fill the command object's Parameters collection with the values from the form for the stored procedure
4) Call the command object's ExecuteNonQuery() function
Viola!
Keep It Simple Stupid! (KISS)
|
|
|
|
|
Hello everyone,
I have some phone to each user and I want to store all photos into memory with key-value pair. Key is the user name and value is the binary photo stream. Any ideas how to store binary photo content into memory? Which data structure is suitable to be used?
thanks in advance,
George
|
|
|
|
|
You can store images in byte[] or MemoRyStream.
|
|
|
|
|
Cool, Giorgi!
1.
And if total image size is large than physical memory size, Dictonary will automatically use Windows vistual memory? There is no size limitation for Dictionary data structure itself?
2.
How to read the full content of a binary file into byte[] or MemoRyStream? Confused. ??
regards,
George
|
|
|
|
|
You can use Binary Serialization to write an instance of Image class to memorystream. You can call ToArray() method of memoryclass to copy memorystream data to byte[]. You can also use BinaryReader to read file content in byte[].
Not sure about first question.
|
|
|
|
|
Thanks Giorgi,
1.
I am interested in "Binary Serialization to write an instance of Image class to memorystream". Could you show me some pseudo code or recommend me some samples please?
2.
If I read image into memorystream, then the whole image will be in memory? My confusion is for object stored in memorystream as you suggested solution, will all content be hold in memory or just a handle hold in memory which points to the physical image file?
regards,
George
|
|
|
|
|
George_George wrote: And if total image size is large than physical memory size
Then you shouldn't be storing it in memory.
George_George wrote: How to read the full content of a binary file into byte[] or MemoRyStream
As always, and as you've been told an obscene number of times, you read the bloody docs[^] and/or search for relevant examples[^]!
|
|
|
|
|
Thanks J4amieC,
1.
Could Dictionary use virtual memory?
2.
I read a couple of samples, but either for VB or for ASP.Net, which is not my needs. My purpose is simple, just write a console application is fine. And I do not care about the content of image, so I do not care whether use Image class or not. (the samples all use image class)
I read MSDN sample but seems not describing the same problem for me.
Are there an easy to use sample which just convert image file into memory stream or byte[]?
regards,
George
|
|
|
|
|
As far as I know, when you exceed physical memory, the OS will automatically start to use virtual memory. Your program won't know the difference; it will just start to slow down when you're accessing "memory" pages that have been written to disk.
|
|
|
|
|
Thanks Alan,
1.
So, Dictionary itself does not have any special memory limitations and rely on OS correct?
2.
Could you help to provide or recommend some pseudo code which read a full image into memory and store it in memory or byte[] please?
regards,
George
|
|
|
|
|
|
Thanks Alan!
The link in 2 you recommended is for BMP file, I am working on non-BMP file.
Actually I do not care about the specific file format, I just care about how to read the whole content of a binary file into memory, something like this.
Any ideas or sample recommendations?
regards,
George
|
|
|
|
|
It will work for any file type supported by .NET: .bmp, .jpg, .png, .gif, .tif. They all produce a Bitmap structure in memory. The extension tells .NET what file type it is.
|
|
|
|
|
Thanks Alan!
1.
But it does not work for arbitrary file type, like Word and ppt, correct?
2.
Currently, if I do not care about the content of the file, I think I could use the following method to read all content of a image into memory, correct?
byte[] imageData = System.IO.File.ReadAllBytes(filePath);
regards,
George
|
|
|
|
|
1. Correct; just the types supported by .NET.
2. Yes.
|
|
|
|
|
I think you need to read about the concepts of memory management in operating systems, especially about Virtual Memory[^] - there's no connection between a data structure and virtual memory. And thinking about using the full address space of a process is always a very bad idea
regards
modified 12-Sep-18 21:01pm.
|
|
|
|