Browsing 7239 questions and answers with Jon Skeet

Why is variable not printing correctly?

Why did the following program output Method B 0 instead of Method B 200? I can't understand what my problem is. class A{ int a=100; void myMethod(){ ...
Jon Skeet
people
quotationmark

Instance field initializers run after the superclass constructor has been called. So the order of execution is: Call to new B() Start to initialize instance of B: Implicitly call super() Start to initialize the instance with respect to... more 6/26/2017 5:04:13 AM

people

Sending large image through TCPClient c#

I have the following code to send a picture to a receiving application public static void sendFile(string file, string ip) { using (TcpClient client = new...
Jon Skeet
people
quotationmark

You're only calling Read once, which certainly isn't guaranteed to read all the bytes. You could either loop, calling Read and copying the relevant number of bytes on each iteration, or you could use Stream.CopyTo: var imageStream = new... more 6/25/2017 1:36:45 PM

people

Scope of a variable cfr. Pluralsight C# test

I'm preparing for my microsoft exam about c# 70-483, "still a long way to go" and following the C# path on pluralsight. After doing the test and review my incorrect answers i came...
Jon Skeet
people
quotationmark

The question is vague and badly worded IMO. There's no such concept as a variable "going out of scope" in C# - but there is the scope of a variable, and the scope of the i variable is the whole of the foreach loop body, including the empty... more 6/24/2017 8:04:29 AM

people

How do I sort list of numeric strings without parsing?

I have a list of strings with high values (greater than int 32), How do i sort them in ascending order without parsing? List =...
Jon Skeet
people
quotationmark

Firstly, I almost certainly wouldn't take the approach below. I'd either convert the input to a List<long>, or just use the code you've already got, at least until I'd absolutely proved that it wasn't good enough. However, as this... more 6/23/2017 5:28:23 PM

people

Removing Duplicate Nodes from Xml using C#

I have an xml like <xml> <Test> <TestData> <TestData> <Name>Alex</Name> </TestData> </TestData> </Test> ...
Jon Skeet
people
quotationmark

LINQ to XML makes this reasonably easy if some assumptions are met: There are no elements with "triple duplication" e.g. <TestData><TestData><TestData>. I'm sure it's feasible to work around that, but it's trickier. We... more 6/23/2017 4:04:33 PM

people

Array.equal() giving wrong output

To my understanding, the following code should print true, since both elements are equal. From java docs Array.get() will return: Returns the value of the indexed component...
Jon Skeet
people
quotationmark

This has nothing to do with arrays really. Your comparison is equivalent to: Object x = Integer.valueOf(3); Object y = Byte.valueOf((byte) 3); boolean equal = x.equals(y); That's never going to return true. Even though your original... more 6/23/2017 2:01:15 PM

people

Generic types and ienumerable<T>, where to start

I want to pass in a dataset to a function but it could be different each time (but will always implement IEnumerable. So my call to the function will be: var items = new...
Jon Skeet
people
quotationmark

You're trying to use T as a type argument, so the compiler needs to know which T you mean. Chances are you want to make your method a generic method too. Fixing the name to follow conventions (and be more readable in general) at the same... more 6/22/2017 10:17:19 AM

people

an expression tree lambda may not contain a null propagating operator

Question: The line price = co?.price ?? 0, in the following code gives me the above error. but if I remove ? from co.? it works fine. I was trying to follow this MSDN example...
Jon Skeet
people
quotationmark

The example you were quoting from uses LINQ to Objects, where the implicit lambda expressions in the query are converted into delegates... whereas you're using EF or similar, with IQueryable<T> queryies, where the lambda expressions... more 6/21/2017 4:19:47 PM

people

How to remove ambiguous method call in c#

Here I have a simple scenario with two methods where I get an ambiguous invocation from one another: This is a my code: public IEnumerable<JobsViewModel>...
Jon Skeet
people
quotationmark

The only way to do this would be to fill in the optional parameter with a value of the appropriate type, so that the compiler knows which overload to pick. For example: public IEnumerable<JobsViewModel> GetJobsViewModels( Guid... more 6/21/2017 10:31:49 AM

people

System.Net.WebException remote name could not be resolved 'http'

I am using System.Net.HttpWebRequest to make a request to a server by setting the proxy property of the object as: webRequest.Proxy = New WebProxy("10.x.x.x", port); it works...
Jon Skeet
people
quotationmark

You're calling the WebProxy(string, int) constructor - where the string is meant to be a host name, not a URI. You should call the WebProxy(string) constructor, at which point the string is a URI. If you need to specify a non-default... more 6/21/2017 7:26:26 AM

people