Archive for the ‘.NET’ Category
Posted by jpluimers on 2014/01/30
A while ago, I was refactoring some C# 1 code that uses HashTables as a poor mans property bag.
The problem was that I felt my code was convoluted, and should be denser, especially avoiding Convert.ChangeType. My code was already much simpler than casting tuples to a superclass.
So I asked this question on StackOverflow: c# – Is there a solution that feels less clumsy than Convert.ChangeType to get the value from a HashTable – Stack Overflow.
User dasblinkenlight showed it could be shortened and explained why (hyperlinks are mine):
Since System.String is sealed, the expression
genericType.IsSubclassOf(stringType)
is the same as
genericType == stringType
Therefore you do not need a call of Convert.ChangeType: you can cast to T by casting to object, like this:
object stringResult; // Note the change of type to "object"
if (haveValue)
stringResult = ((string)properties[propertyName]).Trim();
else
stringResult = string.Empty;
result = (T)stringResult; // It is allowed to cast object to generic T
The original .NET 1.1 code had loads of null checks wrapped if/then/else statements to assign default values for null values.
I wanted to get rid of that, and get code like this: 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, Development, Software Development | Leave a Comment »
Posted by jpluimers on 2014/01/29
In the .NET/C#: fun with enums and aliases part 1 you saw that an enumerated type can specify an underlying type.
The underlying type is limited to a strict set of built-in C# types: , so you cannot use a CTS type for it.
So you might think that you can only define enumeration values by integer constant like this:
namespace BeSharp
{
enum TwoState
{
False = 0,
True = 1,
}
enum ThreeState
{
False = 0,
True = 1,
Unknown = -1,
}
}
Well, you can do it like this too, since Operations between different enum types are allowed in another enum declaration: 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, Development, Software Development | Leave a Comment »
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.
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.
Posted in .NET, C#, Delphi, Development, Jon Skeet, Software Development | 1 Comment »
Posted by jpluimers on 2014/01/21
I remember doing this in DOS ages ago (in the Turbo Pascal 5 era) for exactly the same reason: flash the keyboard LEDs to indicate some event was happening, but I’ve yet to find back the source code.
Here is how to do it in Windows using either C# or C: Faking num lock, caps lock and scroll lock leds – About My Code.
–jeroen
via: c# – Way to turn on keyboard’s caps-lock light without actually turning on caps-lock? – Stack Overflow.
Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, C++, Development, Pascal, Software Development, Turbo Pascal | Leave a Comment »
Posted by jpluimers on 2014/01/16
This is one of the things I tend to forget, as you do it once per machine, and the place to do it is not logical to me.
I mainly use it to quickly record a Macro (boy, I wish Office had a TemporaryMacro feature like Visual Studio had. Alas no more: Macro Recording/Playback has been removed in Visual Studio 2012).
The logical place for me would be to have a context-menu on the ribbon where you can enable the Developer tab.
Anyway, this is how to enable the Developer tab in Word 2007/2010/2013: Read the rest of this entry »
Posted in Development, Office, Office 2007, Office 2010, Office 2013, Power User, Software Development, Visual Studio 11, Visual Studio 2002, Visual Studio 2003, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010, Visual Studio and tools, Word | Leave a Comment »
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 »
Posted by jpluimers on 2014/01/09
Reading c# – Type.IsSubclassOf does not behave as expected – Stack Overflow, I found this very interesting link via Assembly Load Contexts Subtleties at Sasha Goldshtein’s blog (I love the name of the blog: All Your Base Are Belong To Us).
He explains the reasons for the error message
System.InvalidCastException: Unable to cast object of type ‘Plugin.MyPlugin’ to type ‘Plugin.MyPlugin’.
Actually his blog entry is an abstract of a 200+ page thesis on that is also recommended reading: Flexible Dynamic Linkin for .NET (by Anders Aaltonen, Alex Buckley and Susan Eisenbach).
–jeroen
via:
Posted in .NET, .NET 3.5, .NET 4.0, .NET 4.5, .NET CF, C#, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »
Posted by jpluimers on 2014/01/07
Thanks Pedro! (emphasis below is mine)
The ContextSwitchDeadlock doesn’t necessarily mean your code has an issue, just that there is a potential.
If you go to Debug > Exceptions in the menu and expand the “Managed Debugging Assistants”, you will find ContextSwitchDeadlock is enabled.
If you disable this, VS will no longer warn you when items are taking a long time to process.
In some cases you may validly have a long-running operation.
It’s also helpful if you are debugging and have stopped on a line while this is processing – you don’t want it to complain before you’ve had a chance to dig into an issue.
–jeroen
via: c# – Visual Studio: ContextSwitchDeadlock – Stack Overflow.
Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, Development, Software Development, Visual Studio 11, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010, Visual Studio and tools | Leave a Comment »
Posted by jpluimers on 2014/01/01
For a project, I needed to strip the potential Path.DirectorySeparatorChar and Path.AltDirectorySeparatorChar.
Where Path.Combine will combine two paths and insert the DirectorySeparatorChar, I could not find the opposite, so I wrote this little piece of code:
using System.IO;
namespace BeSharp.IO
{
public class PathHelper
{
public static string RemoveTrailingDirectorySeparators(string directory)
{
string result = directory.TrimEnd(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
return result;
}
}
}
–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, Development, Software Development | Leave a Comment »
Posted by jpluimers on 2013/12/29
Thanks Uncle Bob Martin for posting this.
I’ve been trying (with increasing success: it takes time to get this all right) to practice XP (through various name changes) as much and wide as possible since almost 14 years, and only the last few years it is starting to be common practice for many more people.
take a moment to reflect back on 1999. A time when Kent Beck wrote a ground-breaking book. A book that changed everything. Look back and remember: Extreme Programming; and recognize it as the core of what we, today, simply think of as:
Good Software Practice.
–jeroen
via: Extreme Programming, a Reflection | 8th Light.
Posted in .NET, Agile, Continuous Integration, Delphi, Design Patterns, Development, Software Development, Source Code Management, Technical Debt, Testing, Unit Testing | Leave a Comment »