Browsing 7239 questions and answers with Jon Skeet

AEM CQ System.currentTimeMillis() returning repeated results

I'm working on a AEM project, and I have a component X. Along this component, I have another one which is a container of X. So, I can drag X instances and drop them onto the...
Jon Skeet
people
quotationmark

Is that possible? Absolutely. For a start, it's very possible for a computer to do more than one thing in a millisecond. For a second thing, the clock consulted here often won't have a granularity of milliseconds - you may find that... more 12/11/2014 4:21:23 PM

people

How to use String.Replace

Quick question: I have this String m_Author, m_Editor But I have some weird ID stuff within the string so if I do a WriteLine it will look like: '16;#Luca Hostettler' I...
Jon Skeet
people
quotationmark

It sounds like you want a regex of "at least one digit, then semi-colon and hash", with an anchor for "only at the start of the string": string author = Regex.Replace(m_Author, @"^\d+;#", ""); Or to make it more reusable: private... more 12/11/2014 1:50:42 PM

people

How do I cast from Nullable<T> explicitly

i have defined custom value type MyCustomValueType with implicit cast operator from long to MyCustomValueType. public struct MyCustomValueType { private readonly long...
Jon Skeet
people
quotationmark

Why does the compiler even allow explicit casting no one is defined. What rules does the compiler apply here? It applies the lifted conversions defined in section 6.4.2 of the C# spec: Given a used-defined conversion operator that... more 12/11/2014 12:56:55 PM

people

Unreported exception java

I am getting an unreported exception; must be caught or declared to be thrown error in the fill method below. From similar posts I read I am assuming that the error originates...
Jon Skeet
people
quotationmark

Your fillData calls buffer.readLine(), which is declared to throw IOException - but you neither catch the exception witin fillData, nor declare that it might be thrown. The simplest fix is to change the signature of fillData to: public... more 12/11/2014 11:48:15 AM

people

Difference between two overloads of Enumerable.Except?

I am trying to understand the difference between two overloads of Enumerable.Except method i.e. Except(IEnumerable, IEnumerable) Except(IEnumerable,...
Jon Skeet
people
quotationmark

Two reasons: You can only implement IEquatable<T> for your own types... whereas you can specify an IEqualityComparer<T> for any type, whether you can modify it yourself or not. There may be multiple ways of comparing a type... more 12/11/2014 11:44:28 AM

people

Anonymous methods Scope c#

I wanted to know that where can we define the anonymous method (anonymous functions and lambda statement) because on some websites its written only in function and in some it is...
Jon Skeet
people
quotationmark

You can use anonymous functions pretty much anywhere, including field initializers - but for instance field initializers, you can't use this. So for example: public class Foo { private int x; private Func<int> y = () =>... more 12/11/2014 11:24:37 AM

people

C# return value of (int) +=

In C#, you can use int Previous = x++; to load the value of x, before being incremented, into Previous (Previous=0, x=1). However, int Previous = x += 5 does not behave the same...
Jon Skeet
people
quotationmark

Is there a suitable shorthand statement to increase an integer by an interval larger than 1, while storing the original variable, that I'm unaware of? No, there's no general compound post-increment operator. You could fake it with a... more 12/11/2014 10:45:59 AM

people

linq query to group the items

var sales = _salesService.GetSales(parameters) The list is something like var listSales = new List<SalesData>(); listSales .Add(new SalesData { Name = "Apple", ...
Jon Skeet
people
quotationmark

Well, it sounds like first you need to know the total quantity. That's easy enough: var total = listSales.Sum(entry => entry.Quantity); Then you need to work out the cutoff point, which is 1% of it: var cutoff = total / 100; For... more 12/11/2014 8:49:36 AM

people

Closure access to private constructor in C#

I understand how in C# closures allow access to private variables declared in the same scope as an anonymous method, so that those variables are available when the method is...
Jon Skeet
people
quotationmark

In this case, the compiler doesn't need to create a class at all - it can just create a static method: public static Func<SomeClass> GetFactoryMethod() { return __GeneratedMethod; } private static SomeClass... more 12/10/2014 5:33:46 PM

people

read digits (instead of int) from a txt file in java

I have a file like this test.txt: 122352352246 12413514316134 Now I want to read each digit a time, and store it as one element in, say, a list. I tried using Scanner. But it...
Jon Skeet
people
quotationmark

I guess there must be an elegant way to directly read one digit each time. A digit is a character - so you want to read a single character at a time. Oddly enough, Scanner doesn't appear to be conducive to that. You could read a... more 12/10/2014 5:08:44 PM

people