|
The hashcode is only used balance the hash table.
What doesn't work is if A.GetHashCode() != B.GetHashCode() but A.Equals(B). That is, it may work accidentally but all bets are off.
That means that GetHashCode could return 0 and everything will still work. Performance will suffer though.
edit: useful link: Eric Lippert on GetHashCode[^]
|
|
|
|
|
Excellent explanation, Thanks for that.
Thanks
Md. Marufuzzaman
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
The key value is what is used for definining the functionality; the hash value is used for optimizing the performance.
IDictionary requires all keys to be different, that is all. Different keys may result in the same hash value, as the key can have any size, and a hash is just a 32-bit integer, so you can't have an infinite number of different hash values.
And please use PRE tags, I don't read unformatted code...
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.
|
|
|
|
|
 CORRECTION - Dictionary did throw an ArgumentException if insert two items (where "Equals" returned true) gets inserted into dictionary twice (just set "j" to a bigger number, say 2)
However, below I've tried to implement GetHashCode in two ways:
ATTEMP 1 (correct because "GetHashCode" aligned with "Equals") --- 46 seconds to recall the elements by Key
ATTEMP 2 (Incorrect because not aligned with implementation in "Equal") --- 49 seconds (Seems like negligible and diff really arise from the addition XOR operation in GetHashCode)
You can try to implement GetHashCode = a^b, it'd be done in no time.
I'm not really sure why we need "GetHashCode" still - (unconvinced that its uses it's primarily internal to Dictionary's internal retrieval and impact only speed)
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;<br />
<br />
namespace TestDictWithDuplKeys<br />
{<br />
public class SomeClass<br />
{<br />
public int a = 0;<br />
public int b = 0;<br />
public int c = 0;<br />
public int d = 0;
<br />
public override bool Equals(object obj)<br />
{<br />
SomeClass cObj = null;<br />
<br />
if (obj != null && obj is SomeClass)<br />
{<br />
cObj = (SomeClass)obj;<br />
if (this.a == cObj.a && this.b == cObj.b && this.c == cObj.c)<br />
{<br />
return true;<br />
}<br />
else<br />
{<br />
return false;<br />
}<br />
}<br />
else<br />
{<br />
return false;<br />
}<br />
}<br />
<br />
public override int GetHashCode()<br />
{<br />
int HashCode = a ^ b ^ c;
return HashCode;<br />
}<br />
}<br />
class Program<br />
{<br />
static void Main(string[] args)<br />
{<br />
IDictionary<SomeClass, string> SomeDict = null;<br />
SomeClass o = null;<br />
const int MAXITEMS = 1000000;<br />
try<br />
{<br />
#region Add to dictionary, with duplicate keys!<br />
SomeDict = new Dictionary<SomeClass, string>(MAXITEMS);<br />
for (int j = 0; j < 1; j++)<br />
{<br />
for (int i = 0; i < MAXITEMS; i++)<br />
{<br />
o = new SomeClass();<br />
o.a = i;<br />
o.b = i * 2;<br />
o.c = i * 3;<br />
o.d = i * 4;<br />
<br />
SomeDict.Add(o, Guid.NewGuid().ToString());<br />
}<br />
<br />
Console.WriteLine("Pass #" + j);<br />
}<br />
#endregion<br />
Console.WriteLine("Finished adding elements to dict....");<br />
<br />
#region Retrieve from dictionary<br />
DateTime Start = DateTime.Now;<br />
foreach(KeyValuePair<SomeClass, string> Entry in SomeDict)<br />
{<br />
string Value = SomeDict[Entry.Key];<br />
}<br />
#endregion<br />
DateTime End = DateTime.Now;<br />
<br />
Console.WriteLine("Duration(sec):" + End.Subtract(Start).TotalSeconds);<br />
}<br />
catch (Exception Ex)<br />
{<br />
Console.WriteLine(Ex.Message);<br />
Console.ReadLine();<br />
}<br />
<br />
return;<br />
}<br />
}<br />
}<br />
dev
|
|
|
|
|
Hello Everybody,
I am developing an application in which have we are providing Full Company Address to access or print.They are fixed and not need to be change.
So i am confusing where to place these FullAddress (XMLFILE,DataSet,Database,Or Arraylist).
So pls suggest me where is the best location to place these 1000 FullAddresses.
If you can think then I Can.
|
|
|
|
|
If the application uses a database, it would be simple to add an address table with a column companyID that links to another table company . Then you could retrieve the address as and when you need it. It would be no good keeping it in a In-Memory structure such as DataSet / ArrayList as you would need to Hard Code the values in the code. A viable alternative is to persist the addresses using XML, if the application is not using a database. Obviously, this would depend on the application itself.
Hope this helps.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
|
|
|
|
|
eg_Anubhava wrote: They are fixed and not need to be change.
Experience shows, this is unlikely.
Where to place them is a tricky one. I'd narrow it down to XML or a database, that way they can be changed/added to with little effort (and used, for example, in word for mail-merge) and without needing a re-compile.
You might want to google "LINQ To SQL" or "Entity Framework" for two simple ways of getting the data out in a C# app. LINQ to SQL is probably easier for your requirements.
Hope this helps!
|
|
|
|
|
Dear Sir,
Think that you have 1000 Rules of Government that you want Store. Then what will you do. Because there is no need to be insert.
and there no need to install SQL for client machine.
The Client Just want to View and Print and Filter these records.
If you can think then I Can.
|
|
|
|
|
Well, I'd do it in a database anyway, databases are designed to store data. XML is a less helpful choice, but both either can then be used by other sources (as opposed to hard coding).
eg_Anubhava wrote: Think that you have 1000 Rules of Government that you want Store. Then what will you do. Because there is no need to be insert.
Until the 1001th rule is added.
|
|
|
|
|
But For Database Option Database should be installed on Client Computer.
If you can think then I Can.
|
|
|
|
|
|
Not necessarily. It could be installed on a server instead.
|
|
|
|
|
Or just a text file.
From your other comments my guess is that you don't have a database already.
So some sort of file is going to probably be the best solution.
After you have decided on a file then the format of the file could be XML. Or something else that you make up.
You could embedded them in the application itself as a resource or as code as well. This of course means that updating them requires updating the application. But there could even be business requirements to do that.
|
|
|
|
|
Normally, There is a table "CompanyMaster" in most of the solution. There every company related direct information is stored.
|
|
|
|
|
Hy,
I have the following application:
- server application that uses a digital certificate to login to a website; first time i run the app it requires the pin number from the token... certificate is stored on token); i use X509CertificateUI to select the certificate and it pops-up the token client to enter pin number;
- client application connects to my server app and send some data which the server passes to the website to get response and send back to the client.
Everything is working ok. BUT... i want to make it a windows service. (currently is form based). I have done the service part, i installed it, can start it from the service manager, but it doesn't show the certificate selection and the token client for the pin.
here is the server code to get the certificate;
var store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection selection = X509Certificate2UI.SelectFromCollection(store.Certificates,
"Digital Certificates",
"Select a certificate from the following list:",
X509SelectionFlag.
SingleSelection);
After i select it and enter the token pin number it works ok, but haven't been able to do the same as a windows service. I have also exported the certificate into a file from the token and declared a x509cert from local file but it should also require the token pin number.
X509Certificate2 localCert = new X509Certificate2(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\my.cer");
return localCert;
thanks
|
|
|
|
|
I believe you have to install the service as the user who's certificate will be used. I bet you're installing it as Local System or something like that.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
You're betting wrong. It does not matter how i install the service. I tried with Local System and with User but i get the same issue.
Using the X509Certificate2UI throws exception: "The current session is not interactive"... which is kinda obvious
Using the X509Certificate2 from file I get "Cannot create secure SSL/TLS channel" because it does not prompt me to enter the token pin number.
|
|
|
|
|
Services aren't supposed to launch UI windows. It looks like the certificate API is respecting that.
|
|
|
|
|
I already saw that... hence the "which is obvious...". I was asking if there is any other solution to pass the pin code.
|
|
|
|
|
Have you tried using a datafile to store the pin code and have the windows service read that file?
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
You probably already googled your issue, but I did find this (search phrase is "C# windows service load 509 certificate"):
http://msdn.microsoft.com/en-us/library/ms731899.aspx[^]
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
trying to paste clipboard contents to another application window(word doc or ms paint) using the folowing code
if (process != null && !process.HasExited && Clipboard.GetImage() != null)
{
Win32.SendMessage(process.MainWindowHandle, Win32.WM_PASTE, IntPtr.Zero, IntPtr.Zero);
}
it doesn't work.
is anything wrong with the above approach?
SendMessage and WM_PASTE does it work for sending the clipboard contents to another running process or it works only to Paste the contents of the Clipboard into a control
any idea?
- Regards - J O N
A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers
|
|
|
|
|
John P Harris wrote: it doesn't work. is anything wrong with the above approach?
Dunno, might be caused by isolation[^].
I are Troll
|
|
|
|
|
IMO the problem is the destination document isn't the main window of the target app; in Word you would CTRL/V in the document pane, not just anywhere inside the whole window.
Suggestion: for simple apps it could work; for MS Office, I'm afraid the official way (through Primary Interop Assembly is what you need.
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.
|
|
|
|
|
i have to import xml document having size more than 10GB in Sql Server database. i also have to do XSD Validation before Processing and maintain transaction. Please suggest me the best way to do the same. should i use SSIS packege? is it capable of Processing such large document? or Do i have to use c# coding to accomplish this task?
|
|
|
|