Browsing 7239 questions and answers with Jon Skeet

Unity3D C# Cannot implicitly convert type `string' to `int'

I am converting my .js list to c#. This is what I have: List<string> allflipping = new List<string>["020-030-031","032-033-023","013-003-002","001-000-010"]; And I...
Jon Skeet
people
quotationmark

It's not clear why you've put that in square brackets. It's as if you're trying to create an array of List<string>, using your strings as the size of the array. I suspect you meant to use a collection initializer with curly... more 1/24/2014 7:12:14 AM

people

toArray() method in Collection class

In Collection class are 2 toArray() methods: <T> T[] toArray(T[] a) and Object[] toArray(). There is no E[] toArray() method. Why? It is connected with type erasures, but...
Jon Skeet
people
quotationmark

There is no E[] toArray() method. Why? There's no way it could actually create an E[] at execution time, because it wouldn't know the type of array to create, due to type erasure. The add method will really accept anything, but the... more 1/23/2014 7:28:00 PM

people

XmlDocument garbled when opening and saving via StreamReader/StreamWriter

I have a WinForms application in C# that uses XML configuration files. To prevent other apps and users to edit those files while the app is running I use... To open...
Jon Skeet
people
quotationmark

This is the problem: new FileStream( fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite ) If the file already exists, that will overwrite the existing file, but will not truncate it. You should specify... more 1/23/2014 5:45:24 PM

people

What is "T..." on Java?

Looking at the methods of the Array class on libgdx. I found this method: public void addAll (T... array) { addAll(array, 0, array.length); } I had never seen that "T..."...
Jon Skeet
people
quotationmark

The T... is just a varargs parameter, where the element type happens to be T, the generic type parameter of the class. The point is that you can call the method like this (assuming array is an Array<String>): array.addAll("x", "y",... more 1/23/2014 5:15:30 PM

people

convert byte array to hex in c#

I need to run a stored procedure from code. One of the input parameters is rowVersion of the table. rowVersion is a byte array ( {0, 0, 0, 0, 0, 0, 13, 191} that's...
Jon Skeet
people
quotationmark

I suggest you add it as a byte array. For example: byte[] bytes = new byte[] { 0, 0, 0, 0, 0, 0, 13, 191 }; cmd.Parameters.Add("@myPKRowVersion", SqlDbType.Binary).Value = bytes; If you're trying to specify bytes, the most natural type... more 1/23/2014 3:01:15 PM

people

Merging dictionaries with similar keys but distinct values in C#

Consider the following dictionaries: Dictionary<string, double> dict1 = new Dictionary<string, double>() Dictionary<string, double> dict2 = new...
Jon Skeet
people
quotationmark

This assumes they really do have the same keys: var merged = dict1.ToDictionary(pair => pair.Key, pair => new[] { pair.Value, dict2[pair.Key] }); Or to create a Dictionary<string,... more 1/23/2014 2:49:13 PM

people

Difference in generated SQL

The following c# code: Func<Customer, bool> predicate1 = s => s.Name == "Roger"; dbContext.Customers.Where(predicate1); generates this sql query: select col1,col2...
Jon Skeet
people
quotationmark

The difference is that in the first case you're calling Enumerable.Where with a delegate (Func<Customer, bool>). The LINQ provider itself doesn't see that at all - Enumerable.Where will just iterate over the whole of the... more 1/23/2014 2:42:54 PM

people

How to use LINQ to check if all dates are coming from same month

I have one date filed in one object . I want to check if all dates are coming from same month [ Month can be anything ]. Below are the example for checking coming date are from...
Jon Skeet
people
quotationmark

Well you could use something like: bool allSameMonth = @Model.Events .Select(x => x.EventDate.Month) .Distinct() .Count() < 2; (I've taken the liberty... more 1/23/2014 1:30:29 PM

people

StreamWriter not writing to file when called from task Scheduler C#

I have the following function, that accepts a string, and logs the string content to a log file. private static void LogEvent(string sEvent) { sEvent =...
Jon Skeet
people
quotationmark

You're currently trying to write to the process's current working directory - which may well be something like C:\Windows\System32 when it's executed by the task scheduler. You're not going to be able to write there. Specify an absolute... more 1/23/2014 1:18:18 PM

people

How to read text file after specified line in c#

I have a huge text file which i need to read.Currently I am reading text file like this.. string[] lines = File.ReadAllLines(FileToCopy); But here all the lines are getting...
Jon Skeet
people
quotationmark

Well you don't have to store all those lines - but you definitely have to read them. Unless the lines are of a fixed length (in bytes, not characters) how would you expect to be able to skip to a particular part of the file? To store only... more 1/23/2014 11:58:23 AM

people