Browsing 15711 questions and answers with Jon Skeet

C# Method reference of constructor

In C# I can have references of methods and static methods. Can I also get the reference of a classes constructor? In Java I can say Supplier<MyClass> createMyClass =...
Jon Skeet
people
quotationmark

No, there's no equivalent of method group conversions for constructors, or properties, indexers or operators. It's an entirely reasonable idea, but it isn't in C# at the moment. It is, however, tracked as part of a feature request in the... more 5/23/2017 6:42:46 AM

people

this is Sparta, or is it?

The following is an interview question. I came up with a solution, but I'm not sure why it works. Question: Without modifying the Sparta class, write some code that makes...
Jon Skeet
people
quotationmark

But why does Sparta in MakeItReturnFalse() refer to {namespace}.Place.Sparta instead of {namespace}.Sparta? Basically, because that's what the name lookup rules say. In the C# 5 specification, the relevant naming rules are in section... more 5/22/2017 8:02:07 PM

people

Partly negative period between 2 dates with NodaTime

I use NodaTime library to get period between two days, but sometimes I get partly negative period between two dates: var start = DateTime.Now; var end =...
Jon Skeet
people
quotationmark

TL;DR: Update to Noda Time 2.0.2. This was a bug in 2.0.0 and 2.0.1 of Noda Time. Brief testing suggests it wasn't an issue in earlier versions. The problem was due to an optimization when trying to obtain the time parts of a period... more 5/22/2017 4:26:34 PM

people

Java Static Initializer Block datatype

static int B,H; static boolean flag = true; static { Scanner scan = new Scanner (System.in); B = scan.nextInt(); H = scan.nextInt(); scan.close(); And...
Jon Skeet
people
quotationmark

Well in your second code, B and H are local variables within the block - you're not assigning to the field at all. That's exactly the same as if you were writing a method... Forget static initialization for the minute - imagine you had... more 5/22/2017 6:17:45 AM

people

How to create delegate for Result property getter in Task c#

I need to create a delegate for Result property of Task in c#( TResult getter_Result) I am able to get getter method for property Result Resultgetter =...
Jon Skeet
people
quotationmark

You can do this via expression trees, but the way your sample code is set up makes it look like you want just a Func<object>, which means the task has to be already embedded in the expression tree. You can do that: using... more 5/21/2017 6:56:12 PM

people

To Sum Value using linq

In the below code i have a list i am trying to get values from list using linq query and sum the values.But i don't know how to sum the values.So please help me to resolve the...
Jon Skeet
people
quotationmark

You shouldn't be including the value in your grouping - you want to group by just Desc and Month (I assume), then sum the value parts. Two options: var sums1 = objList .GroupBy(y => new { y.Desc, y.Month }) .Select(group =>... more 5/21/2017 6:20:01 PM

people

What is the difference between .net Core multi target and .net Standard?

.Net Standard is used to can be use the same library in different type of projects, such as WPF, xamarin, UWP... etc. .Net Core can't by default, but there is the possibility to...
Jon Skeet
people
quotationmark

You would need to target multiple frameworks in the csproj file. In the original launch of Visual Studio 2017 there's no UI for this, but you can do it manually. I believe that there will be UI support for this in an update. It's just a... more 5/21/2017 11:15:21 AM

people

Convert Utf 8 String To Windows Cp 1258

I want to convert a text file from utf-8 to windows cp-1258 by C# console, here is my code but it does not work, the output file content is not cp-1258 string path =...
Jon Skeet
people
quotationmark

Your current code is going through all kinds of steps it doesn't need to. All you need is: string text = File.ReadAllText("input.txt"); Encoding encoding = Encoding.GetEncoding(1258); File.WriteAllText("output.txt", text,... more 5/21/2017 8:39:22 AM

people

Generic type function (property)

I'm trying to return a T type value. public T this[int index] { get { if (typeof(T) == typeof(int)) return (T) dynEx[index].Int; ...
Jon Skeet
people
quotationmark

In order to cast to int, you'll need to cast the result to object first, potentially boxing. It's annoying, but unfortunately it's a limitation of C# generics - for good reasons that escape me right now, unfortunately. So you want: public... more 5/20/2017 8:32:31 PM

people

pass byte[] , file parameter to a method java

I m confused to how to pass byte array and file to a method in Java : when I call enregistre method : enregistre(img3File, file3) and enregistre(img4File, file4) ...
Jon Skeet
people
quotationmark

Look at what your enregistre method does: BufferedOutputStream stream; stream = new BufferedOutputStream(new FileOutputStream(new File("ben1"))); stream.write(imgFile); stream.close(); If you call that method multiple times, it will... more 5/20/2017 5:00:11 PM

people