Browsing 7239 questions and answers with Jon Skeet

FileWriter vs FileOutputStream in Java

I'm little confused about FileWriter and FileOutputStream. As I see source code of FileWriter there are just 4 constructors and each constructor is calling FileOutputStream's...
Jon Skeet
people
quotationmark

FileWriter is a Writer. It's about writing text - and it happens to be writing it to a file. It does that by holding a reference to a FileOutputStream, which is created in the FileWriter constructor and passed to the superclass... more 3/27/2016 5:51:58 PM

people

Constraints on Type Parameter only elements which occur in an array of types

I have this code: Type[] typList = GetAllTypesFromAssembly(Assembly.LoadFile("aaaa.dddd.dll")); And this statement, which matches if my desired type will be in that...
Jon Skeet
people
quotationmark

You can't - because type constraints are designed to be verifiable by the compiler, whereas clearly typeList.Contains(typeof(T)) is only checkable at execution time. The simplest approach is probably just to validate it as a precondition... more 3/27/2016 4:58:50 AM

people

In Java, inside a static method, what is the way to determine which class the static method was called on?

Suppose you have a class and a subclass in Java: public abstract class Car { public static Car getCar() { Car car = makeCar(); return car; } private...
Jon Skeet
people
quotationmark

So in the Car's getCar() method, how can I tell whether the method call was sent to the class Ferrari, as opposed to the class, Car You can't. (I thought javac used to generate the same bytecode for both calls - it now doesn't, but I... more 3/24/2016 8:56:56 PM

people

Sha512 not returning equal in c# hash validation

The 64 bytes are the same, but the == is returning false. I am guessing there may be something wrong with the encoding, but I can not figure out what I am doing wrong. Any ideas...
Jon Skeet
people
quotationmark

No, there's nothing wrong with the encoding - there is no encoding in a byte[]. They're just bytes. It's just a matter of == for arrays performing a reference comparison. You could use SequenceEqual to compare the arrays: public static... more 3/24/2016 8:28:07 PM

people

c# weird Dictionary ContainsKey or StringComaprer

It's some kind of weird magic, ContainsKey returns false. I tried to use InvariantCulture comparer with the same result. GameCommands = new Dictionary<string,...
Jon Skeet
people
quotationmark

You have zero-width spaces (U+200B) at the start of some of your strings. For example, copy this: {"​Drop card", Drop}, into the Unicode Explorer here, and you'll see something like this: Now, we don't know where that character came... more 3/24/2016 4:32:33 PM

people

How do i find how many times a substring is used in a string?

public Object countOccurrences(String string) { int e = 0; int i = 0; while ( i < sentence.length()) { if(sentence.contains(string)) { ...
Jon Skeet
people
quotationmark

What is wrong with my code that won't allow me to pass the test? Before doing anything else, you should consider how you could have worked out what was wrong for your code to start with. Did you debug through it? At what point did it... more 3/24/2016 7:18:00 AM

people

How to get name of variable via their value in class C#

Currently I have a class as below: public static class JurisdictionList { public const string NSW = "New South Wales"; public const string NT = "Northern Territory"; ...
Jon Skeet
people
quotationmark

Well you could use reflection: public static string GetJurisdictionCode(string jurisdiction) { return typeof(JurisdictionList) .GetTypeInfo() .DeclaredFields .Where(f => (string) f.GetValue(null) ==... more 3/24/2016 6:55:50 AM

people

Unity3D: Return value with a Delegate

I want to be able to get the return values from all the methods in my delegate. This is the code I have written in c#. using UnityEngine; using System.Collections; ...
Jon Skeet
people
quotationmark

Well, in the "regular" .NET framework, you could use Delegate.GetInvocationList. For example, to combine that with LINQ: // Note: do all of this after checking that unitSpawn is non-null... var results = unitSpawn.GetInvocationList() ... more 3/23/2016 5:07:23 PM

people

Powershell Base64 String to Bytes

I am trying to convert a File with Powershell to a Base64 encoded Byte Array which is required by a webservice. How can i do that? My current approach is to : $content =...
Jon Skeet
people
quotationmark

If soapObject.byteArray is expecting a byte array, I'd expect to just be able to give it the array - let the proxy perform the encoding in base64. So: $soapObject.byteArray = content No need for base64String at all. more 3/23/2016 3:38:40 PM

people

C# loop loops the first statement three times before moving to the second statement

I have wrote this do-while loop in a console app in C#: do { ThisHelp.ShowMenu(); userChoice = (char) Console.Read(); ...
Jon Skeet
people
quotationmark

Console.Read() generally only returns when the user has pressed return - at which point the carriage return and line feed will also be returned by subsequent calls to Console.Read(). You can validate that by logging (or examining in the... more 3/23/2016 3:18:08 PM

people