The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,862 other subscribers

Archive for the ‘Jon Skeet’ Category

.NET/C#: some starting posts on the `async` and `await` keywords

Posted by jpluimers on 2014/12/02

Last year’s summer, I posted some .NET/C#: some starting posts on the yield keyword and mentioned that async and await might be the most complicated compiler transform.

So here are some posts to learn more about async and await: Read the rest of this entry »

Posted in .NET, .NET 4.0, .NET 4.5, C#, C# 5.0, Development, Jon Skeet, Software Development | 1 Comment »

Delphi, C#, VB.NET and SQL all have escapes to use reserved words as identifiers

Posted by jpluimers on 2014/11/04

Normally you would not want to use a reserved word as an identifier. But sometimes it can be very convenient, for instance for a code generator that wraps remoting calls or does ORM.

Both Delphi and C# have an escape for this:

The prefixes are to tell the compiler knows you really know what you are doing, and are using a reserved word as an identifier.

The cool thing: in the Run Time Type Information (Delphi) or Reflection (C# and VB.NET) you will see the names without the prefix.

Some examples from StackOverflow: Read the rest of this entry »

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Conference Topics, Conferences, Delphi, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 8, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Delphi XE5, Delphi XE6, Delphi XE7, Development, Event, Jon Skeet, Software Development, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 7.0, VB.NET 7.1, VB.NET 8.0, VB.NET 9.0 | Leave a Comment »

StackOverflow/StackExchange: two queries to help me improve my questions and answers

Posted by jpluimers on 2014/09/05

On the StackExchange network, I try to keep track of the questions I asked, and answers I gave in order to see if they need follow up.

For instance, questions with answers that have no accepted answer usually indicate there is room for improvement.

On my own questions, it means I could accept an answer, or give out more information on how to better answer that question.

On mu answers to questions from others, it might mean I need to improve my answer, or comment on his question to explain it better.

The first category can be queried easily by the built in search capabilities:
http://stackoverflow.com/search?q=user%3ame%20is%3aanswer%20hasaccepted%3ano

The second category requires the Stack Exchange Data Explorer (SEDE) to execute some SQL:
My answers to questions that have no accepted answer – Stack Exchange Data Explorer.

  • Replace the ‘me’ with your StackOverwlow user ID
    (for StackOverflow.com, you can get that from http://stackoverflow.com/search?q=user%3ame as it will be listed in the search box there)
  • replace stackoverflow with a mnemonic obtained from the main SEDE page (click on the icon you are interested in, then copy that part into the query)

Note some questions (like Wiki: Current state of the art of Delphi 3rd party TCP/IP components libraries – Stack Overflow) are not suited to have one ‘best’ answer.

Note the SEDE can be addictive. If it is starting to become that way, perform a Jon Skeet comparison.

Note the SEDE data is usually at least a few days older than the live data.

--jeroen

Posted in Development, Jon Skeet, Pingback, Power User, SocialMedia, Software Development, Stackoverflow | Leave a Comment »

.NET/C#: an alias is not the same as a synonym: int versus Int32, etc (fun with enums and aliases part 1)

Posted by jpluimers on 2014/01/28

I was amongst the C# programmers that believe the below table of C# integral types is an alias table. But it is not: it is a synonym table.

C# type .NET Framework Type/
Common Type System
byte System.Byte
sbyte System.SByte
short System.Int16
ushort System.UInt16
int System.Int32
uint System.UInt32
long System.Int64
ulong System.UInt64

You can use the common type system types as coding standard, or prefer the C# types. There are arguments for both.

I am still in the first group: prefer CTS types. They make changing between .NET languages a lot easier (C# and VB.NET are not the only .NET based CLI languages I use).

And indeed almost everywhere you can exchange the .NET Framework Type and the C# type without changing meaning.

There are two exceptions Read the rest of this entry »

Posted in .NET, C#, Delphi, Development, Jon Skeet, Software Development | 1 Comment »

.NET/C#: you cannot do string? because Nullable is for value type, and string is a reference type

Posted by jpluimers on 2014/01/15

At clients, I see quite a few people being confused by this compiler error message:

Error 1 The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable'

One of the reasons about the confusion is that a string variable behaves like a value type, but in fact is a reference type because their values can consume a huge amount of memory (thanks User codekaizen).

A few interesting questions on that on StackOverflow:

Anyway, back to the error message above.

Lots of people are confused by it, just see a few questions on StackOverflow: Read the rest of this entry »

Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Jon Skeet, Software Development | Leave a Comment »

A small table that shows differences between decimal, double and float (Single)

Posted by jpluimers on 2013/10/16

Though you programmers all should have read What Every Computer Scientist Should Know About Floating-Point Arithmetic.

But I know you don’t, so below is a small difference on which floating point comparisons fail when using float (f, Single), double (d) and decimal (m, money) values in C#:

0.1 fd_cast_none fd_cast_up fm_cast_none dm_cast_none
0.3 fd_cast_none fd_cast_up fm_cast_none dm_cast_none
0.7 fd_cast_none fd_cast_up fm_cast_none dm_cast_none
0.5 fm_cast_none dm_cast_none
0.9 fd_cast_none fd_cast_up fm_cast_none dm_cast_none

The can help you decide what kind of floating point type you want to use, for instance to answer c# – When should I use double instead of decimal? – Stack Overflow.

I specifically choose the values 0.1, 0.3, 0.5, 0.7 and 0.9 to stress the difference between binary and decimal representations. Apart from the decimal type, you cannot store these decimal values in a binary representation. You can see the decimal representation for a double using the DoubleConverter class (thanks Jon Skeet!)

If you are have a problem that isn’t suite for floating point, then don’t use it. Use rational types, IntXBigInteger or Complex from the System.Numerics namespace, or arbitraty precision floating point numbers.

The failing methods are part of a bigger DecimalDoubleSingleTestProject, for which you will find the source at BeSharp.net repository (you can browse the sources, and access it through SVN and TFS).

That project contains more checks (see the table at the end which includes 0.100000000001 and 0.1222222222222222222221 based on the accuracy you can get with float/double/decimal) and the failing/succeeding methods are the same.

This is what the failing fd_cast_none, fd_cast_up, fm_cast_none and dm_cast_none methods do:

//...
    [TestClass]
    public class UnitTestBase
    {
        protected float f { get; private set; }
        protected double d { get; private set; }
        protected decimal m { get; private set; }
//...
        public void TestMethod_fd_cast_none()
        {
                Assert.AreEqual(f, d);
        }
//...
        public void TestMethod_fd_cast_up()
        {
                Assert.AreEqual((double)f, d);
        }
//...
        public void TestMethod_fm_cast_none()
        {
                Assert.AreEqual(f, m);
        }
//...
        public void TestMethod_dm_cast_none()
        {
                Assert.AreEqual(d, m);
        }
    }

The methods seem to succeed, but that is just the deceiving part: when you carefully select the values you compare, all checks will eventually fail.

The table at the end shows some more literals that fail other tests. It is caused by all these types have different storage formats.

<h3>Conclusion</h3>

When comparing floating point literals, make sure they are of the same type, and select the type according to what precision or representation features you need.

Note this gets even worse when you start calculating with floating points. You will almost always loose accuracy, watch rounding errors and you cannot even do direct AreEqual comparisons any more. Read the articles by Eric Lippert tagged floating+point+arithmetic – Fabulous Adventures In Coding.

--jeroen

Full table:

0.1 fd_cast_none fd_cast_up fm_cast_none dm_cast_none
0.3 fd_cast_none fd_cast_up fm_cast_none dm_cast_none
0.5 fm_cast_none dm_cast_none
0.7 fd_cast_none fd_cast_up fm_cast_none dm_cast_none
0.9 fd_cast_none fd_cast_up fm_cast_none dm_cast_none
0.100000000001 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none
0.300000000003 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none
0.500000000005 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none
0.700000000007 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none
0.900000000009 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none
0.1222222222222222222221 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_up
0.3222222222222222222223 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_up
0.5222222222222222222225 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_up
0.7222222222222222222227 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_down dm_cast_up
0.9222222222222222222229 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_up
144444444444444444444.1 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_down dm_cast_up
344444444444444444444.3 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_up
544444444444444444444.5 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_up
744444444444444444444.7 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_up
944444444444444444444.9 fd_cast_none fd_cast_up fm_cast_none fm_cast_up dm_cast_none dm_cast_up

Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, Algorithms, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Floating point handling, Jon Skeet, Software Development | Leave a Comment »

.NET/C#: UnitPrefixes class that facilitates distinguishing decimal and binary file/drive/memory size (mega versus mibi, etc)

Posted by jpluimers on 2013/08/14

Everyone knows there is a size difference between a gigabyte of memory, and a gigabyte of disk space.

The former is 102410241024, the latter is 100010001000.

To facilitate this, I’ve created a C# class UnitPrefixes containing quite a few constants and readonly values.

The class is below, but a few interesting facts first:

  • Most values are const, but a few are readonly static variables because they cannot calculated at compile time (the C# compiler by design does very limited calculations at compile time; it is complex enough as it already is).
    As Jon Skeet explains, there are some other differences between const and readonly static, which is why I favour const.
  • Though all consts are positive, I could have used UInt32 and UInt64, but the .NET framework favours signed Int32 and Int64 types for parameters, so to avoid casting, I used the signed ones.
  • There is no Int128 or UInt128, but there is System.Numerics.BigInteger which I use for values too large for 64-bit integers.
    Note that BigInteger is relatively new, so this code will only work in C# 4 or higher, and requires .NET 4 or higher.
    This is also the place where I use the public readonly static fields, as I need to call the BigInteger constructor to initialize it.
  • I used the Decimal type, as the mantissa holds up to 28 digits of accuracy.

I used the Wikipedia pages Binary Prefix and Metric Prefix (I could also have used File Size) for the unit names and abbreviations.

Note that BitsPerByte is a const I needed too, and I will probably add constants for 512 and 4096, as you see those often in computing as well.

The below sample code is also available as a changeset on BeSharp.CodePlex.com. Read the rest of this entry »

Posted in .NET, .NET 4.0, .NET 4.5, C#, C# 4.0, C# 5.0, Conference Topics, Conferences, Development, Event, Jon Skeet, Software Development | Tagged: , | Leave a Comment »

CLR week on the Old New Thing has started: first one about Static Constructors

Posted by jpluimers on 2013/08/06

Finally a new CLR Week on the Old New Thing.

It starts with via: Why does BitConverter.LittleEndian return false on my x86 machine? which is all about static constructors.

So you should read these as well:

(Note that virtually anything from Eric Lippert and Jon Skeet it worth reading anyway).

--jeroen

via: Why does BitConverter.LittleEndian return false on my x86 machine? – The Old New Thing – Site Home – MSDN Blogs.

Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, Development, Jon Skeet, Software Development | Leave a Comment »

C#/.NET/LINQ: DotNetStat: netstat.exe with TCP ports grouped by host

Posted by jpluimers on 2013/06/18

A lot of people have written .NET equivalents of netstat code. Basically there are two starting points:

I adapted the first, made the output very much like the built-in Windows netstat, and added some LINQ code to demonstrate grouping and ordering.

Now you get grouped output like this:

Distinct Remote Address:Port pairs by Remote Address:
107.20.249.140   443
107.20.249.78    443
127.0.0.1        6421, 19872
192.168.1.81     17500, 61678
199.47.218.159   443
199.47.219.148   80
199.47.219.160   443
23.21.220.140    443
23.23.127.94     443

The code below is part of the DotNetStat example.

It demonstrates a few important LINQ aspects beyond the LINQ Query Expressions (C# Programming Guide) intro and 101 LINQ Samples in C#.:

Read the rest of this entry »

Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Jon Skeet, Software Development | Leave a Comment »

.NET/C#: Class declarations initially set are indeed different than setting them in a constructor (via: Stack Overflow)

Posted by jpluimers on 2013/01/16

Thanks to Danny Thorpe and John Skeet, I learned something about C# initialization order.

I knew there were differences between declarations having their initial value set at the point of declaration, and inside a constructor, but not about all of them.

So I observed the initialization order while stepping through code, but the virtual method behaviour was new to me.

Thanks Blaz Art for asking this at SO.

Danny Thorpe: Read the rest of this entry »

Posted in .NET, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Jon Skeet, Software Development | Leave a Comment »