Browsing 7239 questions and answers with Jon Skeet

encapsulation, accessor C# vs Java

A quick question about encapsulation and accessor in C# vs Java. Is this code in C# equivalent to the one in Java below? C#: class MyClass{ public string var1 {get;...
Jon Skeet
people
quotationmark

No, in the C# code you've got private setters - in your Java code they're public. The C# equivalent to the Java code would be: public string Var1 { get; set; } public int Var2 { get; set; } (I've changed the names to follow .NET naming... more 6/2/2015 7:06:48 PM

people

How to save an array of floats and shorts in byte to disk in java?

I have two arrays that I'd like to save to file. An array of floats and an array of shorts (mesh data). I don't want to save these as characters because it makes the files...
Jon Skeet
people
quotationmark

I'd probably use DataOutputStream - write out the number of vertices, then the vertices themselves. Then write out the number of indices, then the indices. This will always use big-endianness, and should be easy enough to read/write on... more 6/2/2015 6:32:55 PM

people

SimpleDateFormat API displays wrong Date

Tried formatting few dates using SimpleDateFormat API String[] dates={"18-01-2015","9-02-2015","21-03-2015"}; for(String s:dates){ SimpleDateFormat format=new...
Jon Skeet
people
quotationmark

You want dd instead of DD when you construct the SimpleDateFormat. DD means "day of year", not "day of month". Every time SimpleDateFormat looks like it's doing the wrong thing, you should consult the documentation and check your pattern... more 6/2/2015 5:52:49 PM

people

Why are IEnumerable Projections not Immutable?

Assuming my Awesome class has a property Bazinga, which is a String with the default value of "Default value", I could go: var enumerableOfAwesome = Enumerable ...
Jon Skeet
people
quotationmark

My question however is why the enumerator implementations do not return immutable objects How would you expect them to do that? They're sequences of whatever type is used - what would you expect to happen with something like: var... more 6/2/2015 3:19:57 PM

people

Clarification for Await/Async and Task

Recently I've started working with multithreading and have been trying to better understand await/async and Task. I've typed up the following scenario: private void...
Jon Skeet
people
quotationmark

My question is this: Is it possible to keep using await async in the CreateStuff method and still wait for the completion of the CreateStuff() method in button1_Click() before displaying the message box? Absolutely - but you need to... more 6/2/2015 3:13:11 PM

people

How to use the Func<Task<T>> Syntax?

I found this class in another answer Best way in .NET to manage queue of tasks on a separate (single) thread. I wanted to try it out, but the syntax is a bit strange to me. ...
Jon Skeet
people
quotationmark

To add to the other answers which suggest using Task.FromResult to create a task, you could also use an async lambda expression - this would be useful if you want to use await within the body of the lambda... more 6/2/2015 2:39:12 PM

people

java mandatory package declaration

In java, why is it mandatory for each class to declare the package it belongs to. I mean, package hierachy is already described/enforced using the folder structure of the file...
Jon Skeet
people
quotationmark

I mean, package hierachy is already described/enforced using the folder structure of the file system. Not necessarily. While it's certainly a good idea to organize your code that way, it's not a requirement. Also bear in mind that... more 6/2/2015 2:34:37 PM

people

Why upcasting can be done directly in c#, but in order to downcast we require expilicit casting?

Consider two simple classes public class Parent { public void ShowData() { } } public class Child : Parent { public void GetData() { } } // Upcasting...
Jon Skeet
people
quotationmark

Why UpCasting does not require explicit casting ? Because it's always safe. It's never going to throw an exception. Why DownCasting requires explicit casting ? Because it's not safe. It can throw an exception, or lose... more 6/2/2015 12:44:28 PM

people

Operator precedence and associativity in C#

I've two lines of coding as below int? i = 1; int j = i ?? 2 +1; now "j is 1" int? i = 1; int j = (i ?? 2) +1; now "j is 2" Could you explain how?
Jon Skeet
people
quotationmark

Sure - it's simple a matter of precedence. The (Microsoft) C# specification lists operator precedence in section 7.3.1 (in the C# 4 and C# 5 specifications, anyway; it's 7.2.1 in the C# 3 spec), although that's only really an informative... more 6/2/2015 8:27:36 AM

people

why does this garbage data come with the output?

I have written this program to fetch data as NSArray in ios swift. Why am I getting so much garbage value below in the output besides the required data? override func...
Jon Skeet
people
quotationmark

The "extra" logging is there because you logged it here: func dataOfJson(url: String) -> NSArray { var urla = NSURL(string: url) var data:NSData = NSData(contentsOfURL: urla!)! println(data) // This is printing the hex... more 6/2/2015 6:05:20 AM

people