|
Thanks Luc, but for Dictionary, it's internal Hash table still needs to be stored some where right? I presume allocation resembles that of IList - i.e. a dict would allocate a block of contigious memory where hash+reference to actual elements are stored. (i.e. if you keep adding to it, eventually it'd reach "Capacity" specified by Dictionary's constructor)
Further if LinkedList does the same internally, sounds to me real difference between SortedDictionary and LinkedList, don't you think? (LinkedList needs to store an array of references some where and in which case it must have fixed size, unless internally it uses a List<void*>). Forgot to say, both Dictionary and LinkedList supports "ElementAt($INDEX$)", but LinkedList cannot do lookup by Key
dev
|
|
|
|
|
I suggest you look up "linked list" and "hash table" in Wikipedia, or in a relevant book. The former is normally not implemented with an array, the latter probably uses an array and more.
And if you need to know the details of the (current) implementation in .NET, read the documentation and investigate using a tool such as Reflector.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
hum... I implemented/handcoded linked list before when I was doing C++, just want to know how it's done in dot net, internally (And hopefully don't have to observe on debugger for everything...)
dev
|
|
|
|
|
Further, note
LinkedList constructor - you may not specify Capacity (Suspect internal when you add an element SomeClass to the list, LinkedList class would wrap it inside a "Node" object, and node object would have reference to "Next" node. (It's a double linked list behind the scene)[^]
Dictionary - you *can* specify capacity (I suspect therefore when inserting to last slot, reallocation would occur - not sure grow by how much however.)
dev
modified on Monday, April 25, 2011 7:59 AM
|
|
|
|
|
devvvy wrote: I suspect therefore when inserting to last slot, reallocation would occur - not sure grow by how much however.
I think dictionaries are typically implemented to grow once a certain percent of capacity has been reached, though "capacity" would be somewhat of a misnomer. So maybe once the array is at least 70% full, then it resizes (read about load factor). Also, dictionaries don't need to resize, as they typically contain collision lists... the resizing is done for performance. As far as how much they would grow, that would probably be some constant multiplier (e.g., by 2x each growth). Some implementations allow you to specify these values (growth factor and resize threshold).
If you want to see how .Net implements the Dictionary class, I recommend using Reflector or ILSpy. You might also consider monitoring your memory usage with very large dictionaries. The MSDN documentation might also contain some info about the constants used.
Fixign now. | But who's fixing the fixign? |
|
|
|
|
|
One of the reasons the implementation details are not in the documentation, is because they can change with new versions of .NET. The implementation details are intentionally hidden from the user so they can be modified/improved if needed, while providing the same external functionality (and general performance characteristics).
If you want to explore the details of the implementation for a particular version of .NET, then Reflector/ILSpy/etc is the way to go. But don't count on those details to be the same in the next release.
|
|
|
|
|
devvvy wrote: I'm not quite sure why I'd pick a LinkedList over a Dictionary
Linked lists and dictionaries are very different data structures. Linked lists contain a list of items. Dictionaries contain a bag of key/value pairs. Linked lists are typically ordered by the order items are inserted. You typically access items in a linked list by nodes (current, next, first). In a dictionary, you access values using a key. Dictionaries may use an array and a bunch of linked lists to store data internally. Linked lists will typically use independent nodes that point to other nodes (i.e., nodes are linked using pointers).
As far as why you would choose one over the other, there are plenty of reasons for that (e.g., random access time for a certain element).
Fixign now. | But who's fixing the fixign? |
|
|
|
|
|
AspDotNetDev wrote: Linked lists are typically ordered by the order items are inserted.
That may be the case, since I'm not sure about "typical" usage. However, linked lists have the special feature that it is easy to insert elements in the middle. Therefore, I'd argue that they aren't ordered by insertion order. An array seems more likely to have elements ordered by order of insertion.
But I'm surprised nobody before you pointed out how different linked lists and dictionaries are in terms of algorithmic efficiency and functionality.
|
|
|
|
|
Hi All,
Iam New To C#
I Writed Code Like This
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Data.OleDb;<br />
<br />
<br />
namespace Access<br />
{<br />
<br />
public class Connection<br />
{<br />
OleDbConnection con;<br />
OleDbCommand com = new OleDbCommand();<br />
<br />
public void connect(string connectionstring)<br />
{<br />
<br />
con = new OleDbConnection(connectionstring);<br />
con.Open();<br />
}<br />
<br />
public void Command(string Commandtext)<br />
{<br />
<br />
OleDbCommand com = new OleDbCommand(Commandtext);<br />
<br />
}<br />
public void ExecuteNonQuery()<br />
{<br />
<br />
com.ExecuteNonQuery();<br />
<br />
}<br />
public void CloseConnection()<br />
{<br />
<br />
con.Close();<br />
}<br />
<br />
<br />
<br />
<br />
}<br />
<br />
<br />
<br />
}
Thease Statements are not working Correctly Really I Dont Know What to Do.
Can Any one Help me With A example Code?
Please Help Me.
Arunkumar
|
|
|
|
|
You are not passing/setting the connection to the oleDBCommand. Also the object com is local to the Command method. Take a look at this tutorial[^] on msdn site.
|
|
|
|
|
Arunkumar.Koloth wrote: Iam New To C#
In that case you should spend more time studying and practicing the langauage basics before trying an advanced topic such as this.
Arunkumar.Koloth wrote: Thease Statements are not working Correctly
See my previous comment, you have nothing in the above that actually executes any of the methods in your class.
The best things in life are not things.
|
|
|
|
|
Right, your main problem is in your Command method; just set the CommandText property of the existing com field.
You should also have the Command method accept values for Parameters.
|
|
|
|
|
Hi Arun
Try this
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
namespace Access
{
public class Connection
{
private string getConnectionString()
{
return "connetionString";
}
public static void ExecuteCommand(string command)
{
OleDbConnection con;
OleDbCommand com;
try
{
con = new OleDbConnection(getConnectionString());
con.Open();
com = new OleDbCommand(command);
com.ExecuteNonQuery();
}
catch (Exception ex)
{
}
finally
{
con.Close();
}
}
}
public void MyMethord()<br />
{<br />
Connection.ExecuteCommand("command write here");<br />
}
|
|
|
|
|
Hello
I think I've discovered some kind of bug here:
I have a ListBox control. I fill its content through DataSource property.
First time I set its width equal to ListBox.PreferredSize.Width, no matter what the real preferred width is, it is set to 120.
From the second time use of PreferredSize.Width it works fine: the width is changed according to the content.
class MyCtrl:UserControl
{
ListBox lb;
public MyCtrl()
{
...
lb = new ListBox();
lb.Visible = false;
lb.Width = 50;
this.Controls.Add(lb);
...
}
public ShowMyListBox(string[] data)
{
lb.DataSource = data;
lb.Width = lb.PreferredSize.Width;
lb.Visible = true;
}
}
Interesting... Is it a bug? Is there any way to fix it?
|
|
|
|
|
Until the control has been fully initialized the preferred size cannot be calculated so the default value is returned. Handling the Load event of the user control and testing PreferredSize in there gives the correct value so you may be better of doing your list box initialization in there.
|
|
|
|
|
Hi everyone
could you tell me what are these? and how we can initialize them?
(1) int[][][] array = new int[3][][];
(2) int[,][][] secondarray= new int[3,2][][];
Thanks
|
|
|
|
|
1 is an array of arrays of arrays of ints, initialized to a length of 3 (that is, the outer array)
2 is an two-dimensional array of arrays of arrays of ints, initialized to a size of 3 by 2 (the outer, 2d array)
They are already initialized, so maybe you meant to ask how to initialize the sub-arrays?
For 1, do array[x] = new int[length][];
For 2, do secondarray[x,y] = new int[length][];
|
|
|
|
|
oh Thanks for your answer it was very helpful,I want to know how can I fill them?
for example
int[][] test= new int[3][];
test[0] = new int[5] { 1, 3, 5, 7, 9 };
....
but I dont know about these arrays can you help me?
Thanks again
|
|
|
|
|
Yes it's also in my message
|
|
|
|
|
What David means is: Every time you create an array, you have to initialize it:
int[] test = new int[3];
Declares a variable that refers to an array of integers, calls it test, and assigns an array of three integers to it. You can now use test[0], test[1], and test[2] perfectly happily.
int[][] test = new int[3][];
Declares a variable that refers to an array of arrays of integers, calls it test, and assigns an array of three integer arrays to it. It does not assign the arrays of integers. To do that, you either need a static declaration, or a loop:
int[][] test = new int[3][] { new int[] { 1, 2, 3 },
new int[] { 4, 5, 6 },
new int[] { 7, 8, 9, 10 } };
Or:
int[][] test = new int[3][];
for (int i = 0; i < test.Length; i++)
{
test[i] = new int[3];
}
You need to repeat the process for each layer of array of arrays you add! Beware: arrays of arrays start to take significant amounts of space, very, very quickly...
These are all "jagged" arrays: the inner arrays are not necessarily the same size.
You can also declare rectangular arrays:
int[,] test = new int[3,4];
This declares a rectangular array of 12 elements in total, and assigns them all. You can happily use test[0,0], test[0,1]... etc.
You can combine them as you did in your example:
int[,][] test = new int[3,4][];
Declares a rectangular array of 12 jagged arrays. You have to initialize each of these to a new array of ints before you can use them, either statically, or in a loop as before.
Note: Normally, I would recommend using a foreach loop rather than a for loop, but that is not possible when filling in jagged arrays, as you cannot change the loop variable:
int[][] test = new int[3][];
foreach (int[] inner in test)
{
inner = new int[3];
}
Will give you a compilation error.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
|
your answer was very helpful for me thank you.
but may I ask a question? what is it? and how can I fill it?
double[ , , ,] m_array4 = new double[,,,];
Thanks
|
|
|
|
|
A 4-dimensional array of doubles - and the first time I've seen one.
Like this: m_array4[x,y,z,w] = 1.0;
|
|
|
|
|
Not like that!
If you look back at my previous answer, that is a rectangular array (doubles or ints, bools or TextBoxes, it doesn't matter what the type is) so you have to tell the compiler how big it is!
double[ , , ,] m_array4 = new double[,,,]; Will give a compilation error, because new double[,,,] does not specify all (or indeed any) of the dimensions. Without them, the space cannot be allocated, and it will complain.
double[ , , ,] m_array4 = new double[3,4,5,6]; Will do it, but - remember I said they get big quickly? - it allocates space for 360 doubles...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
Thanks for answer. 
|
|
|
|
|