C# 6 in action

Michael Earls:

I really like the Null propagation feature! C# 6.0 looks like it will be a winner for me.

Originally posted on Jon Skeet's coding blog:

Now that the Visual Studio 2015 Preview is available and the C# 6 feature set is a bit more stable, I figured it was time to start updating the Noda Time 2.0 source code to C# 6. The target framework is still .NET 3.5 (although that might change; I gather very few developers are actually going to be hampered by a change to target 4.0 if that would make things easier) but we can still take advantage of all the goodies C# 6 has in store.

I’ve checked all the changes into a dedicated branch which will only contain changes relevant to C# 6 (although a couple of tiny other changes have snuck in). When I’ve got round to updating my continuous integration server, I’ll merge onto the default branch, but I’m in no rush. (I’ll need to work out what to do about Mono at that point, too –…

View original 1,903 more words

How to implement IEquatable<T> correctly

Michael Earls:

Some great guidance on implementing comparisons on custom classes.

Originally posted on Technique:

Performing object value comparisons is an every-day occurence (for example, comparing an expected value with an actual value in a test assertion). However achieving true value comparison between two reference type objects is not as simple as you might think. To do it properly, you need to implement the IEquatable<T> interface, override two virtual methods of System.Object and implement two operators. That’s five methods in all!  This may seem like a lot of effort for something that is so simple with value types (i.e, structs) but you really don’t have any choice if you want to perform value comparisons on your class instances.

Here are the methods you need to implement/override:

  • IEquatable<T>.Equals
  • System.Equals
  • System.GetHashCode
  • operator ==
  • operator !=

I’ll describe each method in turn using an example class called Product which needs to support value comparisons but currently doesn’t.

First, implement the IEquatable<T> interface

This adds a single method called

View original 530 more words

Code Refactoring – Converting an array with blanks to a List in C#

Update: It seems I could have simply used StringSplitOptions.RemoveEmptyEntries:

userFunctions.AccessoryGroups = reader["AccessoryGroups"].ToString().Trim().Split('|', StringSplitOptions.RemoveEmptyEntries).ToList();

to reduce it to a single line of code.

 

I was writing some code the other day and I had the feeling that there was an easier way to do it. I was converting an array to a List, but I wanted to eliminate any blank strings, so I couldn’t just call myArray.ToList().

Old code:

List<string> accessoryGroupsList = new List<string>();
string accessoryGroups = reader["AccessoryGroups"].ToString();
string[] accessoryGroupsArray = accessoryGroups.Split('|');

foreach(string accessoryGroup in accessoryGroupsArray)
{
    // get rid of any blank strings
    if(accessoryGroup.Trim() != string.Empty)
    {
        accessoryGroupsList.Add(accessoryGroup);
    }
}

userFunctions.AccessoryGroups = accessoryGroupsList;

As you can see, that’s a lot of code. It just seemed like overkill to me, so I had to refactor.

Here’s the final code:

string[] accessoryGroups = reader["AccessoryGroups"].ToString().Trim().Split('|');
userFunctions.AccessoryGroups = accessoryGroups.Where(a => a.Trim() != string.Empty).ToList();

From 7 functional lines of code to 2. I’d say that’s an improvement. It also looks more elegant. And in the end, isn’t it all about elegant code? :)

“Think Positive” – Complaining doesn’t help

After thinking about the last post I made about Sales, I realized that it wasn’t a very positive post. It was more like I was complaining about something and not providing any useful advice or knowledge. I’d like to post more positive entries to my blog and contribute to the world rather than just complain or point out the bad things.

I’ve always grown up around a “Think Positive” attitude as my dad was always reading positive thinking self-help books. When I was in 9th grade, our career exploration class took 6 weeks to study Zig Ziglar’s “See you at the top” book. It was very good for me at the time and contributed to who I am today.

So I’m a bit bummed that I posted that whiny bit about people saving money by shopping at sales. Who cares? What did I contribute to the world? No one and nothing.

While I’m not necessarily saying that I should be as intense as Zig Ziglar, I do think I should brighten up my blog and do more to be positive about the world around me.

So, after taking the post about Sales down, I’ve decided to try a new approach to blogging about non-technical topics…be positive.

I truly believe in the power of positive thinking. If you believe in something and work to make it true, then it will come true. My own personal success is the direct result of the opportunities I’ve been given combined with the hard work I’ve performed to make sure I don’t lose those opportunities. Opportunity isn’t the only factor, you have to recognize opportunity when you see it. You have to learn to take those little steps and work hard for every one until you get to a place where you can look back and feel a sense of accomplishment.

While life isn’t always exactly what you dreamed it would be, it’s always exactly what it should be. Every situation is a learning opportunity, and every opportunity can lead to a better existence.

Lately, I’ve been thinking about why things are the way they are, and I think I’ve learned that I’m just basically the product of my circumstances and decisions. Everything else is just noise. It is what you make it, I guess. I keep looking back at what we had and comparing it with what we have now and I always seem to come to the conclusion that I’ve somehow shorted myself. However, I also feel deep inside me that we’re where we need to be, even if it looks nothing like it “should”.

We don’t live in the same market that we did in my “heyday”, so we’re not living the same lifestyle, but I’m pretty happy about how it turned out. Sure, there are challenges that we face every day, some short term, some long term, but isn’t that the way it is for everyone?

I want to make a contribution, I want to help the situation. I don’t think I want to be a part of the noise.

#IIS HTTP Error 500.19 – Internal Server Error

I just installed a web application on a new server and I received the following error:

HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related configuration data for the page is invalid.

The lines it showed on the web.config were -1: and 0: with no config file information like it would normally display for malformed XML within the configuration file.

Well, after searching the web, I was unable to find a solution. Then I remembered that I was using URL rewriting. I installed the URL rewriting module for IIS 7 and restarted the site. It worked like it should.

Edit: I’m not using URL rewriting, but I do have the 404 Error redirection turned on. Maybe it uses URL rewriting behind the scenes.

Http Error 503 – Service Unavailable with .NET 4 – #IIS

When trying to debug my web service today after upgrading the project to .NET 4, I was told by Visual Studio that it could not debug and to try running the project outside the browser. When I did, I received a 503 Error, Service Unavailable.

I check the Event Log and found this:

The worker process for application pool 'PartsWebService' encountered an error 'Cannot read configuration file due to insufficient permissions
' trying to read configuration data from file '\\?\C:\Windows\Microsoft.NET\Framework\v4.0.30319\CONFIG\machine.config', line number '0'. The data field contains the error code.

Simple enough, I just have to give the correct permissions to that file. It seems that my service’s application pool identity doesn’t have rights. So I’ll navigate to the path that contains the machine.config and set permissions there.

You can’t just set permissions on the parent folder, you have to give permissions to the machine.config file itself. Add IIS_IUSRS from your local machine to the read and execute roles.

Also, load IIS Manager and check your application pool as this error usually stops the application pool if it’s running.

This should clear up any issues you were having.

.NET, C#, and web development topics with a dose of random thoughts thrown in

Follow

Get every new post delivered to your Inbox.