Browsing 15836 questions and answers with Jon Skeet

Can we change wrapper class into primitive data types?

can we change wrapper to primitive ?if not then what is happening in this code int I = Integer.valueOf(46); System.out.println(I); I am not getting any error.
Jon Skeet
people
quotationmark

Yes, this is called unboxing: Integer boxed = 10; // Boxing int unboxed = boxed; // Unboxing Boxing conversions are described in JLS 5.1.7; unboxing conversions are described in JLS 5.1.8. Note that if you try to unbox a null... more 7/28/2017 6:27:08 AM

people

Type casting in Java from char to short

Why can we not pass a char value as an argument to any method that accepts a short parameter, whereas we can pass a char value to another method whose parameter is of int type?...
Jon Skeet
people
quotationmark

Why can we not pass a char value as a argument to any method that accepts a short parameter Because there's no implicit conversion from char to short in an invocation context. whereas we can pass a char value to another method... more 7/27/2017 1:44:23 PM

people

Copy array of one type to array of different type

I was wondering if it's possible to use some sort of Array.Copy method in C# to copy elements of one array to and array of same size but different type. I get a struct from a...
Jon Skeet
people
quotationmark

For some reason, neither Array.Copy nor Buffer.BlockCopy is happy to copy from an int[] to a Foo[], although Array.Copy can go in the other direction perfectly happily. It looks to me like your options are: The kind of copy you showed... more 7/27/2017 8:43:52 AM

people

ValueTuple not working in Razor views with MVC 5

I have an ASP.NET MVC 5 project in Visual Studio 2017 and after successfully adding C# 7 support (which has worked for me so far), I tried using tuples in my Razor views, but I...
Jon Skeet
people
quotationmark

Of course I have installed System.ValueTuple as a nuget package I suspect that's the problem. My guess is that even though you're targeting .NET 4.5.2, you're running on a .NET 4.7 system, and that includes System.ValueTuple in... more 7/26/2017 7:30:09 PM

people

C# populating class array from xml linq results in FormatException

I'm getting a FormatException error "Input string was not in a correct format." error... Employee class is all strings except the Status(int), Type(int) and...
Jon Skeet
people
quotationmark

By calling ToString on an XElement, you're ending up with the whole element as a string, e.g. <Status>4</Status> ... and trying to parse that as an int. I would strongly suggest: Just using the Value property or a cast to... more 7/26/2017 4:10:41 PM

people

Why initialize a char variable with int value?

I want to know why char variables are initialized with an integer, like this: char x = 320; only can takes as maximum value 320 and be a valid declaration. I understand that...
Jon Skeet
people
quotationmark

I understand that every char has a int value thar is represented in ASCII Code. Nope, a char variable represents a UTF-16 code unit. Its valid values are 0 to 65535 inclusive. From section 4.2.1 of the Java Language Specification: ... more 7/26/2017 7:19:45 AM

people

How to change the date of a Calendar instance in Java?

I have a class DayOfWeek which takes a date from the user and prints the day of the week on that date. For example the user will enter his date as: (with the format MM DD...
Jon Skeet
people
quotationmark

You're currently passing an int to SimpleDateFormat... you should pass the Date value. You don't need a Calendar at all for that: // BAD CODE: DON'T USE - KEEP READING import java.text.*; import java.util.*; public class DayOfWeek { ... more 7/23/2017 2:50:25 PM

people

Can't make a function call in in spite of the using statement and extension needed

So I wanted to be able to choose my environment when running dotnet (an .net core mvc-project) from the terminal. I found this post and thought the second highest answer was a...
Jon Skeet
people
quotationmark

Although the namespace is Microsoft.Extensions.Configuration, the extension is in the Microsoft.Extensions.Configuration.CommandLine assembly, which is in the Microsoft.Extensions.Configuration.CommandLine package. You need to add a... more 7/23/2017 12:53:25 PM

people

error 1061. T doesn't contain a definition for <propertyName>

i am having around 7 models who have same properties(atributes). On view page i am using a model(name = commonModel) which contains all those properties and a extra property to...
Jon Skeet
people
quotationmark

You just need to constrain the type parameter T to be derived from your base class: // Names changed to follow .NET naming conventions private T RelocateValues<T>(BaseGrammar baseModel, T tempModel) where T : BaseGrammar { ... more 7/23/2017 12:15:51 PM

people

Is there a way to use an object from a list only if it exists?

I have a List of objects, lets say DataTables. Is it possible to use an object from the list only if it exists? For example, if the TableList contains 5 DataTables named...
Jon Skeet
people
quotationmark

Assuming TableList has an element type that is a reference type, you can use FirstOrDefault() to return the first match or null, then the null conditional operator to only call the method if the target is... more 7/23/2017 11:06:35 AM

people