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,373 other followers

Archive for the ‘C# 1.0’ Category

Yes Dorothy, the .NET System.Array Class can throw you a NotSupportedException

Posted by jpluimers on 2016/09/21

It’s been in the System.Array class forever, but remarkably few people do know that it can throw you a NotSupportedException (for instance when calling Add, Insert, Remove, etc).

It does because it implements IList, but not all methods implemented from IList are valid.

And it also indicates that, as the IList Properties allows for IsFixedSize to return false.

A similar case is there for IsReadOnly: then you cannot even modify the values.

Ever since I started teaching .NET/C# classes almost 15 years ago, I warned:

beware when you use IList as not everybody implements all methods.

–jeroen

via:

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, C# 6 (Roslyn), Development, Software Development | Leave a Comment »

.NET/C#: two ways of creating an empty array

Posted by jpluimers on 2016/08/18

Empty arrays are not used often as arrays usually are about the presence data, not about the absence.

Here are two ways based on the int data type in C# (the original [WayBackexamples [WayBack] are using string, but since string itself is also a kind of array…):

Specify a size of zero:

int[] a = new int[0];

Specify an empty initialisation:

int[] a = new int[] { };

Though many people think arrays are a thing of the past, I think it is one of the first generic types and have their place. For one, enumerating over arrays using foreach is a lot faster in many environments than enumerating over other data types. Another thing is that the fixed nature of arrays can be very beneficial in setting constraints.

That’s why I like the balanced view from Eric Lippert [WayBack] in Arrays considered somewhat harmful – Fabulous Adventures In Coding – Site Home – MSDN Blogs [WayBack]

–jeroen

via:

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, C# 6 (Roslyn), Development, Software Development | Leave a Comment »

c# – How can I create a temp file with a specific extension with .net? – Stack Overflow

Posted by jpluimers on 2016/01/27

You’d think this is a simple question.

Be amazed by the many ways leading into to creating a temporary file with a specific extension.

This list doesn’t even cover all of them:

  • Create a file name based on GetTempPath, a Guid and an extension.
  • Use the TempFileCollection from the Compiler in System.CodeDom.
  • Get a random file name, then change the extension. Loop until it is unique.
  • Use a timestamp to generate unique file names.

All via: c# – How can I create a temp file with a specific extension with .net? – Stack Overflow.

Which one would you choose?

–jeroen

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, C# 6 (Roslyn), Development, RemObjects C#, Software Development | Leave a Comment »

Ensure you know the state of an instance.

Posted by jpluimers on 2015/11/24

A while ago, I came across a class having (among other members) two methods named like this:

  • Start
  • Stop

Within one of the other members of the class, I had to (temporarily) Stop processing, then Start it again.

But I couldn’t, as neither Start, nor Stop would make a record of the state it left the instance in.

Always ensure you know the state of an instance.

So I added the state, and tests to ensure a Stop/Start change was indeed not breaking things.

–jeroen

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, C# 6 (Roslyn), Delphi, Delphi 10 Seattle, Delphi 2007, Delphi 2009, Delphi 2010, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Delphi XE5, Delphi XE6, Delphi XE7, Delphi XE8, Development, Software Development | 1 Comment »

Do not expose properties with writeable fields without a chance to react on the writes.

Posted by jpluimers on 2015/11/04

From a chat with a co-worker a while ago:

I’m not against properties. Just something against properties properties that are objects with writeable fields.

So even if you expose such a property as read-only, it can still get its writeable fields overwritten.

That is a pain when those are part of the state of the underlying object.

In other words: encapsulate your state changes.

Here we solved it by making

  • the type of the property immutable
  • the property writeable
  • react on state changes during the write

Proper encapsulation.

In this case it was a project mixing C# and Delphi, but you can easily apply the above to any language featuring classes and properties.

Another solution would have been to extend the type of the property so it can expose an event that fires during change. Much more convoluted.

–jeroen

Posted in .NET, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, C# 6 (Roslyn), Delphi, Delphi 10 Seattle, Delphi 2007, Delphi 2009, Delphi 2010, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Delphi XE5, Delphi XE6, Delphi XE7, Delphi XE8, Development, Software Development | 2 Comments »

 
%d bloggers like this: