Browsing 7239 questions and answers with Jon Skeet

java class object and Types

Given a class public class A { public static int classVal; } During run-time, does the type A become a reference to a class Object? public static void main(String[] args) { ...
Jon Skeet
people
quotationmark

No, A isn't a reference at all. It's just the class name. A is not an expression in its own right - it doesn't have a value. It can only be part of another expression (like A.classVal or new A()). more 7/7/2015 9:16:21 AM

people

Are duplicated private member variables preferable to a shared protected member?

This is an idiom I have noticed in a C# codebase which I have been working on recently: class Base { private readonly MemberClass _memberVariable; public Base(MemberClass...
Jon Skeet
people
quotationmark

I would consider protected fields harmful - and I would also consider the data duplication harmful, if the values should always be the same. However, the base class could expose the private field's value via a property instead: class... more 7/6/2015 9:20:51 PM

people

How way to operate on single items at an index in group in IGrouping?

I have simplified groupby code below, where I know there exists at most two records for each group, with each group being grouped by the value at index two in string a array. I...
Jon Skeet
people
quotationmark

It sounds like all you're missing is calling ToList or ToArray on the group: foreach (var group in groups) { List<string[]> pairs = group.ToList(); // Now you can access pairs[0] for the first item in the group, //... more 7/6/2015 8:39:04 PM

people

Marking position in a C# list

I have a list of strings which have been read in from a dictionary file (sorted into alphabetical order). I want to create an index of the last position of each starting letter,...
Jon Skeet
people
quotationmark

Well, you could create a dictionary using LINQ: // Note: assumes no empty words Dictionary<char, int> lastEntries = words .Select((index, value) => new { index, value }) .GroupBy(pair => pair.value[0]) ... more 7/6/2015 2:36:21 PM

people

Write raw text in log file using log4net

I have create the custom xml layout using this post. i need log message with character < but in log file it converted to &lt; this is my code sample public class...
Jon Skeet
people
quotationmark

If you really want to do this, you could use writer.WriteRaw("<![CDATA[\n" + loggingEvent.GetExceptionString() + "\n]]>"); or writer.WriteCData(loggingEvent.GetExceptionString()); However, I would strongly discourage you from... more 7/6/2015 12:21:09 PM

people

Junit test cases of non public class

I have a class as following package a.b public class Public_Class{ public void add(){ //logic } } class Non_Public_Class{ public void subt(){ //any logic ...
Jon Skeet
people
quotationmark

How can I access that non public class and my test cases are not in the same package. Change that, basically. It's pretty common practice (at least as far as I've seen) to keep your production and test classes in separate source... more 7/6/2015 10:17:04 AM

people

DispatcherTimer.Tick statement's syntax query

dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick) In above statement i want to know why is it += and not just = I tried to find about it alot but just couldn't...
Jon Skeet
people
quotationmark

Tick is an event. .NET events are basically a representation of the pub/sub model. Event handlers subscribe to a particular event. The event publisher can raise the event whenever they like, at which point all the event handlers are... more 7/6/2015 8:08:56 AM

people

Loss of precision in java

All integer literals are treated as int in java and floating point literals are treated as double in java. Then why does byte b =10; does not give any error but float...
Jon Skeet
people
quotationmark

In the case of int to byte, there's no real concern about a loss of precision, because both types have the same degree of granularity. You'll get an error if you try to convert a literal with a value outside the range of byte to byte. (The... more 7/6/2015 5:50:12 AM

people

Why a double type variable returns Nan?

I am trying to solve a quadratic equation. I have all the variables of type double, and the result of the s variable Nan. double a, b, c, x1, d, x2, s ; Console.WriteLine("Enter...
Jon Skeet
people
quotationmark

Why it is Nan and not double NaN isn't a type, it's a value - and I suspect you're getting it because x2 is negative. The result of the square root of a negative number is undefined in real numbers (as opposed to complex numbers), so... more 7/5/2015 7:06:01 AM

people

C# to VB.NET Conversion : 'Take (of T)' Error

C# code byte[] array = bytes.Take<byte>(num).ToArray<byte>(); VB.NET Converted Code (i tried multiable ones. same result) Dim array As Byte() = bytes.Take(Of...
Jon Skeet
people
quotationmark

I don't have much experience with this, but it looks like if the VB compiler is able to infer the type arguments, it doesn't let you specify them explicitly. Assuming bytes is a Byte array (or an IEnumerable(Of Byte)), you should be able... more 7/5/2015 6:59:27 AM

people