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,860 other subscribers

Posts Tagged ‘region’

File scoped namespaces – C# 10.0 draft specifications | Microsoft Learn

Posted by jpluimers on 2025/05/01

Oops, I thought this had been published a long time ago, but oh well: it is never too late to publish reflections on a C# programming language improvement.

After recovering from my rectum cancer treatments and finally upgrading most of my projects to recent enough C# versions, it was time to catch up on useful little C# language features released during my treatments.

This one is really nice: [Wayback/Archive] File scoped namespaces – C# 10.0 draft specifications | Microsoft Learn.

I wish it had been released much earlier, as it so much reminds me of the unit keyword in Delphi which influenced C# a lot. Well, actually the unit actually started in UCSD Pascal and Turbo Pascal; UCSD Pascal ran on the UCSD p-Machine (more on that in a future blog post), which influenced the Java Virtual Machine, which was based on Java bytecode and a Just-in-time compiler in turn influenced the .NET Common Language Runtime.

There are many examples from other languages, paradigms and frameworks: I love how C# and .NET bring so much programming history together.

In Delphi  it is easy: a source file can contain at maximum one unit (and apart from files included in that source file, no other source files can contribute to that unit) and the filename needs to match the unitname, so the unit is a self contained namespace.

Read the rest of this entry »

Posted in .NET, About, C#, C# 10, Cancer, Delphi, Development, Java, Java Platform, Jon Skeet, Pascal, Personal, Rectum cancer, Rider from JetBrains, Software Development, Turbo Pascal, UCSD Pascal, Visual Studio and tools, vscode Visual Studio Code | Tagged: , , , , , | 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 »