Browsing 7239 questions and answers with Jon Skeet

How do you custom arragement of a dictionary by key in C#?

I have a dictionary Dictionary<string, string> myDict = new Dictionary<string, string>() { { "country", "1" }, { "state", "2" }, { "name", "3" }, { "type", "4"...
Jon Skeet
people
quotationmark

No, you can't rely on the order in a Dictionary. It's implementation-specific, and can change in unexpected ways. In this case, it sounds like really you should have a custom type instead, with Country, State, Name and Type... more 7/16/2015 5:48:35 AM

people

Is it guaranteed that 2 sequential calls to secure random will give different numbers?

Using SecureRandom is there a guarantee that 2 sequential calls will not return the same number? If this is a strict requirement from client API side, is the following loop in the...
Jon Skeet
people
quotationmark

I wouldn't expect there to be. Each call should be independent - requiring the next call to not give the same result as the previous one means it's not as random, IMO. To take a smaller example, imagine we didn't have nextDouble but a... more 7/15/2015 8:10:30 PM

people

how to convert UTC dateTime to local datetime

I have problem with my search range function because we store UTC dateTime in database.for example user store record and it's after midnight in UTC time zone record stamped ex...
Jon Skeet
people
quotationmark

I would strongly urge you to perform the conversion the other way round: in your .NET code, perform the conversion (once) from local time to UTC, making sure you're really applying the right time zone. Then you can pass the UTC values to... more 7/15/2015 4:16:23 PM

people

C#: Subscribing to event fired from another event

I'm trying to raise PropertyChangedEventHandler from a CollectionChanged callback. It gets raised, but it doesn't reach the subscriber. Perhaps I can't just treat an EventHandler...
Jon Skeet
people
quotationmark

In this call: Utils.RaisePropertyChangedWhenCollectionChanged(this, Collection, PropertyChanged, "Collection"); ... you're passing in the current value of PropertyChanged, which is a single no-op delegate. Instead, you want it to raise... more 7/15/2015 4:09:29 PM

people

What happens when a return statement where method calls itself? Recursion.

So I made this simple code with some help of a tutorial. Going through it i am not quite sure what happens at return faktor(tall-1)*tall. in the return statement the method...
Jon Skeet
people
quotationmark

It works the same way a return statement returning a value always works: The expression is evaluated (in this case faktor(tall - 1) * tall) The value is returned to the caller It can't possibly return before evaluating the expression,... more 7/15/2015 1:56:54 PM

people

How to get the OutputStream of a written PDF from iText

I need your help in getting and storing the written PDF from iText in an OutputStream and then to convert it to an InputStream. The code of writing the PDF is below: public void...
Jon Skeet
people
quotationmark

You should change the declaration of out to be of type ByteArrayOutputStream rather than just OutputStream. Then you can call ByteArrayOutputStream.toByteArray() to get the bytes, and construct a ByteArrayInputStream wrapping that. As an... more 7/15/2015 1:29:21 PM

people

LINQ select null values from List

I have a List<string> and it may contain null values in random indexes. I want to check which elements null and select that elements for throwing message. What i am...
Jon Skeet
people
quotationmark

Sounds like you want to first project the strings to index/value pairs, then pick the elements with null values, and project to just the indexes: var nullIndexes = names.Select((value, index) => new { value, index }) ... more 7/15/2015 8:46:24 AM

people

How to use the properties of a passed list of known objects

this might be a simple fix but I can't seem to find anything about it. I am very new to C# if it's not obvious. I'm passing a list of objects from my main method but I haven't...
Jon Skeet
people
quotationmark

Asset isn't a valid expression here, unless you've actually got a variable called Asset somewhere. You want to find out if the name of any property is Asset... and you want to do it on each object, not on the list itself: foreach (var... more 7/14/2015 6:39:46 PM

people

Do unnecessary curly braces reduce performance?

After recently encountering this while programming, I have been wondering about this. Below are 2 snippets which are both legal and compile. Specifically, my question is this.. in...
Jon Skeet
people
quotationmark

Most of the time it doesn't make any difference - and you should definitely code for readability more than anything else. However, curly braces can have an effect on performance, in a surprising way, although it's pretty unusual. Consider... more 7/14/2015 5:41:47 PM

people

Error compiling java with imported classes from a jar

I'm having trouble compiling my Java source file on Linux. My source file (PetersTeam.java) imports classes from a jar stored in the same file location as the source. I'm getting...
Jon Skeet
people
quotationmark

It sounds like the problem is with the way you created your jar file - assuming you did it at the root of your class hierarchy (which you should) you should have used: jar -cvf testjar.jar org/gamelink/game/*.class ... as there... more 7/14/2015 5:20:51 PM

people