Browsing 7239 questions and answers with Jon Skeet

Most appropriate data type to use?

I am creating a C# forms application and on that application needs to be an input box of some sort for users to type in a time in the format hh:mm:ss. This also needs to be able...
Jon Skeet
people
quotationmark

If you're happy to take a dependency on a 3rd party library, I'd suggest using the date/time library I maintain: Noda Time. Unlike the .NET framework, it has a type specifically for a "time without a date" - LocalTime. You can use a... more 10/29/2013 4:50:47 PM

people

Destination file blank after writing with StreamWriter C#

I have written a console application that in itself is working as I would like it to. It's main output works great in console. But the results inside the loop I want written to a...
Jon Skeet
people
quotationmark

When I run that code, I see an exception: Unhandled Exception: System.UnauthorizedAccessException: Access to the path 'c:\Test.txt' is denied. It's not clear how you're running this, but if it's configured as a console app and you run... more 10/29/2013 4:21:35 PM

people

Pass few elements to methods

I've few methods which acept collections of fixed size (e.g. 2, 3, 5). And I can't decide which way is better: public void Foo(IEnumerable<Object> objects) { ...
Jon Skeet
people
quotationmark

The second is much better in my view: It's obvious from the signature that it's expecting 3 values Failures are flagged at compile time instead of execution time more 10/29/2013 2:04:06 PM

people

Defining type and property of that type inside the class

There is a problem to name property and type. Example: public class A { public class B { ... } public B B; // << error } Question 1: Why error? I could solve it...
Jon Skeet
people
quotationmark

Question 1: Why error? You've got two members both called B within the same class. (A type is a member too.) You can't do that, aside from method overloading. From section 10.3 of the C# spec: The names of constants, fields,... more 10/29/2013 10:23:30 AM

people

Get the value from list

I create the list like var list = new List<KeyValuePair<string, string>>(); list.Add(new KeyValuePair<string, string>("1", "abc")); list.Add(new...
Jon Skeet
people
quotationmark

It sounds like you just want: var value = list.First(x => x.Key == input).Value; That's if you're sure the key will be present. It's slightly trickier otherwise, partly because KeyValuePair is a struct. You'd probably want: var pair... more 10/29/2013 10:14:41 AM

people

What does "///<exclude/>" mean?

I have inherited a bunch of C# code. For a couple of method definitions I find ///<exclude/> as the only XMLDoc in front of it. I tried to Google the meaning of that but...
Jon Skeet
people
quotationmark

In NDoc at least, it means that the XML documentation for this member should be excluded. From the documentation: The <exclude/> tag directs NDoc to exclude the current item from documentation. It's not clear to me whether this... more 10/29/2013 8:46:41 AM

people

Invalid character constant JSON

public class Test { public static void main(String[] args) throws ClientProtocolException, IOException { HttpClient client = new DefaultHttpClient(); JSONObject json...
Jon Skeet
people
quotationmark

If this is meant to be Java, you want: json.put("set_boot", true); json.put("synchronous", true); (There are similar problems later on.) Note: String literals are in double-quotes, not single-quotes in Java You're calling a method... more 10/29/2013 7:28:11 AM

people

How does overload resolution work with a null argument?

Why is the method with a parameter of type Object[] called rather than the method with a parameter of type Object when null is passed as the argument? class Demo { void...
Jon Skeet
people
quotationmark

Firstly, it's important to note that the null value is convertible to both Object and Object[], so both methods are applicable. Then it's just a matter of overload resolution. That's described in section 15.12 of the JLS, and section... more 10/29/2013 7:22:15 AM

people

Generating fatal error in Java

Suppose we are writing a Java library, which provides some I/O ulitity functions, for example, a convenient method to read text files as Strings: public class StringReader...
Jon Skeet
people
quotationmark

But what if it doesn't? Then you're in a really bad situation, and you should probably get out of it as quickly as possible. When a JRE is violating its own promises, what would you want to depend on? I'd feel happy using... more 10/29/2013 7:04:56 AM

people

Linq Query Distinct Function do not work with DropdownList

I want to assign Linq Query result to dropdownlist which contain a Distinct function My Code:- var area = de.City_Area_View .Select(m => new {...
Jon Skeet
people
quotationmark

If your set is small enough (so you don't mind fetching all the values from the database), the simplest thing would be to force the distinct part to be performed locally: var area = de.City_Area_View .Select(m => new {... more 10/29/2013 7:01:24 AM

people