How to compare two objects (testing for equality) in C#

Hacktoberfest and the promise of free t-shirts had me looking for a project to help with this month. That’s how I stumbled across GeneGenie.Gedcom, a genealogical library written in C# (read more about it here), and found myself reviewing everything I know about class equality. I focused on implementing some logic to make sure changes were correctly detected, and in order to do that I had to define “equality” for each class that represented some facet of genealogical research. Why was that? Let’s take two instances of a class – one with data that’s fresh from the »

A Tail of Recursion in Erlang (a C# developer's observations)

A group of us at VHT has been meeting weekly, reviewing some Erlang basics and running through examples. Even though it means giving up a lunch hour, over a dozen people have still been showing up to learn and help each other. Two of us were recently challenged to figure out a couple problems in Erlang without using obvious built-in functions: Determine the length of a list. (without using length([1,2,3]).) Reverse the list. (without using lists:reverse([1,2,3]).) We looked into it, and even shared what we found with the group a week later. This »

An explanation of implicit and explicit conversion in C#

Given a method signature that expects two nullable DateTime parameters… protected int CalculateActualDuration(DateTime? startDate, DateTime? endDate) … SamIAm asked on Stack Overflow about how the compiler understands nullable types: I am able to call the method by passing in both a DateTime? and a DateTime. So how does the compiler understand the difference? My interpretation was that the OP was assuming some hidden logic, obfuscated by the compiler, was responsible for allowing both a type and a nullable type. While everything does eventually have to make its way down to the compiler (so yes, it is ultimately handling both types »

The effect of the Obsolete attribute on a class is ignored when there's an interface involved

While marking some code obsolete the other day, I noticed an unexpected behavior (at first glance). Say you have a class, and an interface it implements. The second argument (true) indicates whether or not you’re allowed to call the method, and it means the difference between a simple warning and an error preventing compilation. public interface ISampleClass { void SomeOldMethod(); } public class SampleClass : ISampleClass { [Obsolete("This is the old way. No one uses it anymore. Shame on you!!", true)] public void SomeOldMethod() { // This is old. I should probably remove and refactor, but I'm too scared. Console.WriteLine("Oooooooooooooollllllllllld stuff."); Console. »

An extension method for passing a column name to SqlDataReader.GetFieldValue in C#

I was reading some or another question on Stack Overflow today, and someone happened to reference the SqlDataReader.GetFieldValue method. It’s a generic method that returns the value of a column, given its 0-based ordinal value. using (var conn = new SqlConnection("yourConnectionString")) { using (var cmd = new SqlCommand("SELECT name, age FROM students", conn)) { conn.Open(); using (var dr = cmd.ExecuteReader()) { var name = dr.GetFieldValue<string>(0); var age = dr.GetFieldValue<int>(1); } } } This doesn’t seem very user-friendly to me. What happens if the query changes? If an additional column is added to the beginning, »

Chaining Assignment Operations (or, using a statement as the expression in another statement)

Background I setup the following property in a WinForms Form, to get an EventAggregator instance from Unity. (This is tightly-coupled legacy code that doesn’t currently support dependency injection via ctor.) private IEventAggregator eventAgg; private IEventAggregator EventAgg { get { if (eventAgg == null) eventAgg = UnityInstanceProvider.GetInstance<IEventAggregator>(); return eventAgg; } } The idea was to get a new instance when first requested, then use the existing instance thereafter. ReSharper suggested an edit But then ReSharper suggested this edit: Convert to ‘??’ expression Accepting the suggestion, it modified my code accordingly: private IEventAggregator eventAgg; private IEventAggregator EventAgg { get { return eventAgg ?? (eventAgg = UnityInstanceProvider.GetInstance< »


Under the Hood - Ternary Operator [ ?: ]

Someone posted the following question on Stack Overflow today: (since deleted) Is there a better way according to best practices to do this type of ternary operation without breaking it down into an if block? visitorInfo = string.IsNullOrEmpty(visitorInfo.latitude) ?  DetermineGeolocation(visitorInfo) : visitorInfo; Meaning, without breaking it into a complete if block, is there a way to avoid saying visitorInfo = visitorInfo on a false condition? Maybe something like: DoNothing() or some similar coding construct? I made the following comment, which may not have seemed entirely helpful to him, but it’s accurate. A ternary operation is equivalent to an if/ »