Browsing 7239 questions and answers with Jon Skeet

Extension method priority

I read from https://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx that extension methods with same name and signature as existing ones on the base type are never called,...
Jon Skeet
people
quotationmark

When the compiler searches for extension methods, it starts with those declared in classes in the same namespace as the calling code, then works outwards until it reaches the global namespace. So if your code is in namespace Foo.Bar.Baz,... more 2/24/2015 2:52:08 PM

people

first non repeated character in a string giving wrong character using java?

I have written code to print first non repeated character from string everything is working fine.But while printing its character its giving null.Example from Sting...
Jon Skeet
people
quotationmark

You're using the key-set of the map. You want the entry-set so that you can check the value of each entry. That's what's storing the count - without any need for parsing strings as integers. You should also fix all of your code to use... more 2/24/2015 11:09:31 AM

people

Potential sync issues with Invoke?

I am trying to read value from control in worker thread. public void worker() { while (true) { ewh.WaitOne(); int index = -1; ...
Jon Skeet
people
quotationmark

As I understand Invoke is launched asynchronously. No, Invoke is synchronous, in that it will block until the delegate has completed in the UI thread. It's unfortunate that this isn't clearly documented in Control.Invoke(Delegate).... more 2/24/2015 10:59:32 AM

people

Difference of LINQ .Aggregate with result selector parameter or directly calling method

Is there a practical difference when using the LINQ Aggregate method by passing a resultSelector function or by directly passing the Aggregate result value to the function? Code...
Jon Skeet
people
quotationmark

As far as I'm aware, there is no difference. Looking at my Edulinq implementation, I've implemented the overload without the selector just by calling the overload with a selector, and then with an identity transformation: public static... more 2/24/2015 7:17:15 AM

people

Literal String using "@" in Java?

In C#, there's a way to tell the compiler to interpret a string literally, without consideration of escape characters. string myStr = @"Some literal string, \ doesn't need to be...
Jon Skeet
people
quotationmark

What you're referring to in C# is a verbatim string literal, as opposed to a regular string literal like "foo" - both are string literals, just as "foo" is a string literal in Java, too. No, Java doesn't have any similar feature. It also... more 2/23/2015 5:47:11 PM

people

creating then writing to file c#

in my little console app for c# I have to write to a file, and if it is not there i have to create it. It works fine when the file is already created, but when i try and create...
Jon Skeet
people
quotationmark

File.Create returns a FileStream - but you're not closing that. To be honest, you'd be better off replacing all this code with just: File.WriteAllText(FilePath, filePresent); That will create a file if necessary, and truncate it if it... more 2/23/2015 2:35:00 PM

people

Declare two String arrays, one extending the other in Java

I need to have the following declarations: private static String[] BASE = new String[] { "a", "b", "c" }; private static String[] EXTENDED = BASE + new String[] { "d", "e", "f"...
Jon Skeet
people
quotationmark

Not like that, no. You can use: private static String[] EXTENDED = new String[BASE.length + 3]; static { System.arraycopy(BASE, 0, EXTENDED, 0, BASE.length); EXTENDED[BASE.length] = "d"; EXTENDED[BASE.length + 1] = "e"; ... more 2/23/2015 1:24:59 PM

people

How to format the datetime in c#?

I got the current datetime by using the following code. DateTime dt = DateTime.Now; But it is in the format of, 2015-02-23 17:25:07.123 how to convert this to the format of,...
Jon Skeet
people
quotationmark

"But it is in the format of" No it isn't. It's just a DateTime. If you want a particular text representation, call ToString on it, specifying the format. For example: DateTime now = DateTime.Now; string formatted =... more 2/23/2015 12:01:58 PM

people

Can I force abstract methods to be protected when someone overrides them?

In my abstract class, I have something like this: public Object methodIWantToExpose(){ // ... methodIDontWantExposed() // ... } protected abstract void...
Jon Skeet
people
quotationmark

No. A subclass can always make a method more public. Even if they couldn't do this with the method you have in your class, they could always write: public void callsMethodIDontWantExposed() { methodIDontWantExposed(); } ... so... more 2/23/2015 10:28:50 AM

people

TCP socket programming, strange behavior when read data

I'm writing tcp socket program. When I send string, shorter than previous sending, from client and receive it on server, something strange happening. For example: First I send...
Jon Skeet
people
quotationmark

You're ignoring the amount of data you've read, instead always converting the whole byte array into a string, including any data which is still present from a previous read (or the initial byte array elements). You should have: int... more 2/23/2015 9:53:06 AM

people