Browsing 7239 questions and answers with Jon Skeet

Other ways of writing \ in C#

I have a bit of code that writes to a text document and i want to do this: pathtxt.Text = pathtxt.Text + treeView1.SelectedNode.Text + "\"; but it thinks the \ is the start of...
Jon Skeet
people
quotationmark

\ is used for escape sequences in text literals for C#. The compiler doesn't think it's the start of a comment: it thinks it's the start of an escape sequence. So you either need to use "\\" (i.e. the escape sequence for backslash) or @"\"... more 6/5/2017 4:24:11 PM

people

java.lang package in java

Since java.lang package is automatically imported in all java programs by compiler, why is it necessary to write import java.lang.annotation; statement at the top of the program...
Jon Skeet
people
quotationmark

Because the java.lang.annotation package isn't the same as the java.lang package. They're simply different packages. Imagine if importing one package imported all the packages "under" it - then import java.*; would import almost... more 6/3/2017 7:55:05 AM

people

Is its possible to pass integer value as method arguments in C# console app

In my code static void Main(string[] args) { } what i want pass integer value in method args static void Main(int? args) { }
Jon Skeet
people
quotationmark

You can't - you can only accept strings. However, you can then parse those strings as integers. For example: static void Main(string[] args) { int[] intArgs = args.Select(int.Parse).ToArray(); // ... } Note that this will throw... more 6/2/2017 10:10:36 AM

people

Adding two periods on nodatime

Where listObjAge is a list with multiple periods; Period objTotalPeriod = listObjAge[0].period; for (int i = 1; i < listObjAge.Count; i++) { objTotalPeriod +=...
Jon Skeet
people
quotationmark

It sounds like you're looking for something to normalize the months and days (and weeks?). The existing Normalize method deals with everything from "days downwards" (e.g. hours) so you can use that to start with: public static Period... more 6/1/2017 4:07:27 PM

people

PerformanceCounter CPU usage value allways 0

For below Performance counter, I am always getting % Processor Time is 0, even my CPU shows 100% usage, what could be the reason? PerformanceCounter pc = new...
Jon Skeet
people
quotationmark

It's explained by the remarks in the NextValue() documentation: If the calculated value of a counter depends on two counter reads, the first read operation returns 0.0. Resetting the performance counter properties to specify a... more 5/31/2017 9:27:59 AM

people

Google Cloud API grammar

Using this code I've found on a youtube video (don't know if i'm able to post) : if (File.Exists("audio.raw")) { var speech = SpeechClient.Create(); var response =...
Jon Skeet
people
quotationmark

Assuming that you just want to add SpeechContext to your request, just add instances to the RecognitionConfig's SpeechContexts property: var context = new SpeechContext { Phrases = { "first", "second" } }; var config = new... more 5/31/2017 8:58:36 AM

people

Returning disposable object and getting CA2000 warning

I am trying to return XMLNodeReader to another function but getting CA2000 warning XmlNodeReader obj =new XmlNodeReader(section); return ser.method(obj); If I use the following...
Jon Skeet
people
quotationmark

Well we have no idea what ser.method does, but passing a disposed object into method seems like a bad idea to me. Basically, your "fix" is bad. There are three possibilities here (and probably others, but these are the main... more 5/31/2017 8:35:41 AM

people

Unable to parse String to Date type in iOS swift 3

I'm trying to parse String to date but getting nil value for some specific date. The parsing of 2017-04-21 09:00:00 Tis successfully, but getting nil value for string 2017-05-30...
Jon Skeet
people
quotationmark

You're using hh as the hours specifier. That's a 12-hour value, and "23" clearly falls outside that range. ("09" parses because it's in range.) Use HH instead: dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" more 5/30/2017 1:42:52 PM

people

C# WebClient uploads file that is around twice as large as original

I am testing some file uploads using WebApi 2.0. I created a controller that has an upload method and I also created a console application to perform the upload. I am doing...
Jon Skeet
people
quotationmark

You shouldn't be using StreamReader and StreamWriter - those are for text, and your content isn't text. Just use Stream instead: private async Task ReadStreamAsync(HttpContext context, string filePath) { using (var input =... more 5/30/2017 1:01:47 PM

people

"String was not recognized as a valid DateTime.” error occurs for some exact dates

I have a problem converting Gregorian calendar dates to Persian(Hijri calendar) using default system "en-GB" culture in my ASP.NET MVC Application. I used the globalization tag in...
Jon Skeet
people
quotationmark

Leaving the parsing error aside, this isn't going to achieve what you want to achieve. A DateTime value is always in the Gregorian calendar - trying to parse 31/04/1396 as a DateTime is never going to work because it's not a valid date in... more 5/26/2017 9:05:41 AM

people