Newbie Mistake — Object Equality

I made a newbie mistake.

This is similar to some code I just wrote. Can you spot the mistake?

(HINT: the code compiles; I made a logic mistake not a syntax mistake):

using System;
using System.Collections.Generic;

public class Program
{
static void Main(string[] args)
{
var d1 = new Dictionary<Foo, string>();
var key = "hi";
var foo = new Foo(key);

d1.Add(newFoo(key), "whatever");
Console.WriteLine("d1 key == d2 key? {0}", d1.ContainsKey(foo))
Console.ReadKey();
}
}

public class Foo
{
public Foo(string fooString)
{
_fooString = fooString;
}

public string FooString
{
get { return _fooString; }
}

// ...

private readonly string _fooString = "";
}


Answer:

The problem is with the ContainsKey() method on the d1 object (of type Dictionary).

I expected the result to be “true” because I was thinking that the VALUE OF THE KEY is the same for d1 and d2 (i.e. “hi”).  But what I forgot is that ContainsKey() is not testing for equality of the VALUE OF THE KEY, but for equality of the key itself (the key object).  And in this case, the key object for d1 is a DIFFERENT OBJECT than the key object for d2.

Note a slight subtly, when I refer to my mistakenly comparing the “value of the key” I am NOT referring to the value of the key/value pair.   I am referring to my mistakenly comparing the private literal member field (_fooString).

In pseudo-code: d1.key.stringValue == d2.key.stringValue BUT d1.key != d2.key

Therefore, ContainsKey() returns false.

Beginner Aside: A Dictionary is a mapping of key/value pairs.  Keys must be unique and each key maps to a value (often a value could be another data structure/object).  Dictionary data structures are commonly used when you are working with data that is not stored sequentially (unsorted) and need fast lookup times (at the small expense of data insertion/write times).   Compare this to a List, in which insertion is fast (just plot the new data on the end) but looking up data is slower because the list must be traversed sequentially until you find the data you are looking for.

Ref vs Out in C#

I am in the process of interviewing contractors at work right now and have had the opportunity to ask one of my favorite C# questions:

What is the Difference Between Ref and Out in C#?

To my continued surprise, I have yet to have an interview candidate answer correctly. When I confront them with the question, most candidates will start to confuse pass by reference with pass by value, even though I tell them that both keywords are used for pass by reference. Or they’ll tell me that they’ve used out in some methods but don’t know why.

Grant it — in my opinion, having both keywords is extraneous. In Java, all objects are passed by reference without distinction. Similarly, C++ does not distinguish between the means of reference. C# requires a keyword to pass by reference. But people get confused because there are TWO keywords and BOTH pass by reference.

My guess is that Microsoft probably assumed they were making life easier for the programmer by providing both keywords for pass by reference. The keyword distinction allows the .Net compiler to assure that an argument is initialized/set prior to passing it into the method in the ref case. Or initialized/set inside the method in the out case.

In other words, the difference is that ref requires that an argument be initialized BEFORE being passed into a method. out does not require prior initialization, BUT does require that the implementation of a method set or initialize the argument. BOTH words are used for pass by reference.

I will demonstrate the difference between ref and out by means of some code:

using System;

public class Program
{
   static void Main(string[] args)
   {
     var x = 5;
     PassByValue(x);
     Console.WriteLine("Result of PassByValue is: {0}", x);

     x = 5; // re-initialize to show nothing funny happening
     PassByRef(ref x);
     Console.WriteLine("Result of PassByRef is: {0}", x);

     x = 5; // not needed -- just for demonstration
     PassByOutRef(out x);
     Console.WriteLine("Result of PassByOutRef is: {0}", x);
   }

   static public void PassByValue(int x)
   {
     x += 5;
   }

   static public void PassByRef(ref int x)
   {
     x += 5;
   }

   static public void PassByOutRef(out int x)
   {
     x = 3; // required to re-initialize ds in order to compile
     x += 5;
   }
}

And here is the result:

Pass By Ref vs Out Result

Result of Pass by Ref vs Pass by Out Demo

Multiple Inheritence

Introduction

This post is about multiple inheritance in C++.

But I want to start by dispelling a myth: C# and Java have multiple inheritance too!  Yes,  in C# and Java you can only directly extend one concrete base class.  But in both languages you can implement as many interfaces as you want.  This limitation is designed to overcome some obstacles of the more flexible multiple-inheritance mechanism of C++.  But just because multiple inheritance in these C-derived languages is limited doesn’t mean it’s not existent.  As I will explain, multiple inheritance is important in object oriented design.

Advanced Aside: However, just because a class in any of these languages can extend one or more base classes, does not mean that it should.  Often composition is a better approach then inheritance.  This is especially true in situations where the derived class would need to hide or change the meaning of a function it is inheriting from a base class.  For example, in beginner software textbooks, the relationship between a shape and circles/squares is often used to demonstrate inheritance.  Simply put, a circle or square would seem to inherit from a shape.  But what if we were using a shape class developed by another programmer, who had already defined the shape class with a member function called GetLengthOfLargestSide().  Well, since circles don’t have sides, this method would be meaningless for circles.  So while it might be appropriate for a square class to inherit from this shape class, a circle class should not.

When Does Inheritance Make Sense?

  • Derived classes are going to exactly reuse previously defined methods in a concrete base class:  For instance, if we had two different classes, one for a square and one for a rectangle, rather then having to BOTH define a method to calculate area (length x height), they could inherit from a common, concrete base class that already implements this method once (such as a parallelogram class).
  • Polymorphism: another method requires passing a parameter of one of several types, but the exact type is not known until run-time.
  • Union of Features: This is the use case that requires the multiple inheritance mechanism of C++.  The limitations in modern managed languages like Java and C# prevent this feature of inheritance.  A union of features occurs when a base class inherits different properties from multiple parents.

For instance, here is a union of features:

class Mother
public:
     int MothersChromosomes() { return 23; }
     // ...
};

class Father
public:
     int FathersChromosomes() { return 23; }
     // ...
};

class Child : Mother, Father
public:
     int ChildsChromosomes() { return MothersChromosomes() + FathersChromosomes(); }
     // ...
};

Note in practice this is probably a lousy example of a proper use of inheritance.   Remember earlier I cautioned about using inheritance when the child (base class) needs to delete or hide something it inherits from its parents.  Well, in my example, it is unlikely the child class inherits both the male parts and female parts of its parents, so to speak, unless we are talking about slugs or snails….  so the child would need to arbitrarily hide (or =delete) on of these features..

Advanced Aside: Inheritance might make sense when derived classes share a common set of constant read-only fields to define “magic numbers”, such as the meaning of PI. But note that using this technique in interfaces may be considered a bad design decision, in part because it breaks the “is-a” paradigm for inheritance. In fact, for interfaces, using public constant fields works in Java, but not in C#. Specifically, in Java, interfaces may contain public static final variables. In C#, interfaces may not contain fields at all. Often a better choice in all languages (C++, C#, and Java) is to declare public static fields in concrete classes (such as the PI field in C#’s System.Math class) or to use enumerations.

The C++ Multiple Inheritance Super Happy Path:

When a C++ class inherits from multiple parents (or even multiple ancestors further up the inheritance hierarchy… e.g. grandparents), in which EVERY function is pure virtual and there are NO public or protected fields than the C++ programmer has nothing to worry about. This is essentially the equivalent of having multiple inheritance of interfaces in Java, C#, etc. Obviously this super happy path means forfeiting many of the benefits of inheritance (and multiple inheritance) so I am not suggesting this optimistic scenario is realistic.

Problem With Multiple Inheritance

Finally I get to the meat of my post. I will demonstrate the crux of the C++ multiple inheritance issue by example. You can imagine even more complex scenarios by extending this example (in particular, the clashes I am about to demonstrate could be higher up the inheritance hierarchy).

#include<string>

#include<iostream>
using namespace std;

class English
{
public:
     virtual string MyFavoriteColor() { return "blue"; }
};

class Japanese
{
public:
     virtual string MyFavoriteColor() { return "青"; }
};

class Woops : English, Japanese
{
public:
   void PrintFavoriteColor()
   {
      cout << MyFavoriteColor; // ERROR (ambiguous) -- is this English or Japanese?

   }
 };

Note, the proper way to fix this is to preface the class name and scope operator before the ambiguous call, e.g.:  

cout << Japanese::MyFavoriteColor;

PowerShell %

In a previous post, I demonstrated the Where-Object cmdlet (whose alias is ?).

Another important cmdlet is ForEach-Object.  In fact, it is so important that it also has a simple alias: %

Say for example that you want to calculate the length in kilobytes of every file in the current directory. It’s this simple: Get-ChildItem -File | % {$_.Length / 1KB}

So what’s going on here?

The left side of the pipe (see my previous blog) is enumerating all children (e.g. files) in the current directory. I use the -File switch to limit the results to files. On the right side of the pipe, we use ForEach-Object to loop through each file and calculate the file length, in KB. We wrap the calculation in brackets { and } because this is an expression we need PowerShell to evaluate. Note that because it is such a common unit of file system measurement, PowerShell also understands the notation KB.

Advanced Aside: As I’ve said before, there are many ways to do the same thing in PowerShell. For instance, you could also have done this: Get-ChildItem -File | Select-Object {$_.Length / 1KB}. In this case, the Select-Object mechanism also offers the ability to pretty this up. Try this for instance: Get-ChildItem -File | Select-Object Name, @{Name="Kbytes";Expression={$_.Length / 1Kb}} | ft -AutoSize. But for a quick way to enumerate values of a collection on your PowerShell host, what could be simpler then % ?

Here’s another example:

Let’s say you wanted to enumerate a list of all odd numbers from 1 to 10,000…. This PowerShell host syntax looks a bit convoluted, but you would just type 1..10000 | % { if ($_ % 2 -eq 0) {return} write-host $_ }. To make this easier to read, just imagine the syntax were broken out on multiple lines, like this:

NOTE: this code will not run due to the position of the line breaks:

1..10000 | %         # for each number from 1 through 10,000
{
   if ($_ % 2 -eq 0)    # does current object mod 2 have a remainder of zero
   {
      return               # if its even then do a noop
   }
   ## else
   Write-Host$_         # output current object (i.e. the odd numbers)
}
Advanced Aside: It is possible to divide up code over multiple lines on the PowerShell host, as long as your code follows certain rules regarding the placement of curly braces and such.

PowerShell $_

In my previous post — my first blog ever! — I gave a high level overview of PowerShell. I tried to provide enough material to make it immediately useful.

But in order to see how typing could possibly be more efficient then clicking around with a mouse, I need to introduce a few more basic constructs.

Here is a (contrived) hypothetical: I log onto a production machine that far too many people have access to. And I see something strange. iTunes is installed. Why would someone install iTunes on a production machine? I need to find out who installed it and decide whether I can safely remove it.

The graphical way:

  1. For starters, in Windows 8.1, it just took me almost five minutes just to find the Event Log Viewer Application…. but that’s a rant for another day.
  2. I click on the “Application” log, but see lots of useless metadata (such as a bunch of event entries with a “Task Category” of “None.”)
  3. So I randomly click on a log entry. The bottom half of my screen shows details. Do I have to read all the details for each entry? Should I have chosen another career path?
  4. I move down to the next event log entry. More of the same. The text just blurs together.
  5. Now I notice an option on the right side of my screen to “Filter Current Log…”
  6. But I click on this and am already lost. What could the correct “Event level” be? What about the “Event sources” or “keywords?” This did not help.
  7. This is where I am thinking it’s time to hire an intern.
Windows Event Viewer

Windows Event Viewer Chaos – Now What?

And now the PowerShell way:

  1. I launch PowerShell (I have it pinned to my Windows desktop).
  2. I type: Get-EventLog Application | ? {$_.Message -like "*itunes*"} and get this:
    PowerShell: list of iTunes install events

    PowerShell: list of iTunes install events

  3. Hmmm. Id 152 looks interesting. I type: (Get-EventLog Application | ? {$_.Index -like "152"}).UserName PowerShell thinks for a second and then replies back with this: Peter
  4. So I go talk with Peter and find out he installed it by accident. He promises to be more careful next time.
  5. Life is good.

Advanced Aside: in PowerShell, there are MANY ways to do the same thing. For example, starting with PowerShell V3, I could have typed Get-EventLog Application | ? Message -like "*itunes*" instead of Get-EventLog Application | ? {$_.Message -like "*itunes*"}. Or I could have listed all properties of Index 152, instead of just the UserName. Or I could have formatted the results of my first query as a list instead of a table.

Now to Explain What I am doing in step 2

  • Get-EventLog Application: The word “Application” is called a parameter. With an important exception (called a switch), parameters are name/value pairs, that look like this: Verb-Noun -ParamaterName -ParamaterValue. So in my case I would type Get-EventLog -LogName Application. But when I type help Get-EventLog -Full (note help is short (an alias) for Get-Help) I see a detailed explanation of the parameter -LogName including this information: Position? 1. This means -LogName is what is called a positional paramamter, meaning that if the value appears in a certain position after the cmdlet (in this case first), then the name -LogName is not required. This helps shorten the amount I have to type into the PowerShell host, since PowerShell is all about user efficiency. So instead of typing Get-EventLog -LogName Application, I just type Get-EventLog Application.

    Advanced Aside: -Full is an example of a switch, i.e. it is like a parameter with a name/value pair but it only has a name, no value. In other words, it is a Boolean indicator where it’s absence is like “false” and its presence is like “true.” So when I type help Get-EventLog -Full I am turning on a property (more information) for the cmdlet.

  • | (pronounced pipe): Indicates that the output to the left of the pipe becomes the input to the right of the pipe. So in my case, the result of Get-EventLog Application becomes the input for ? {$_.Message -like "*itunes*"}.
  • ? — The “?” is NOT a typo. It is short (an “alias” for) Where-Object, another cmdlet. In other words, I am taking the results on the left side of the pipe and searching only for results WHERE
  • {$_.Message -like “*itunes*”}: OK, this is the hardest part for me to explain. For starters, the stuff between { and } is called a script-block. It is basically an expression that we are asking PowerShell to evaluate. Buried in the middle of our script block is -like. This means our script block expression is a Boolean expression; within the script block, we are comparing something on the left side of -like to something on the right hand side of -like. In other words, if the something on the left side is ALIKE to what is on the right side then the expression is true. In our case, we are comparing $_.Message to "*itunes*".

    Hopefully the "*itunes*" part is pretty self-explanatory. It’s just looking for any strings (case-insensitive) that contain the text “iTunes.” $_.Message requires some discussion, however. Remember that everything in PowerShell is an object. The syntax “$_” is equivalent to the keyword this in C#. In other words, $_ means the instance of this object (where each object is one row/entry from Get-EventLog Application. The . (period) after $_ tells us we are looking at the property of an object. This is just like many other programming languages. In C#, for instance, we could type string x = "hi"; int length = x.Length where x is an instance of a string (an object) and x.Length is the property Length on the object x. So in summary, $_.Message means we are looking at the property Message on the current instance of whatever object came in from the left side of the pipe (an entry in the EventLog).

    Now things get more fun…. If we run the left hand side of our pipe statement (Get-EventLog Application) by itself, we would find a lot of results. This makes sense. Surely the computer has more than one entry in the event log. So we are actually piping to the right hand side ? {$_.Message -like "*itunes*"} each EventLog entry individually. And the right hand side of our pipe is outputting to us every EventLog entry in which the -like expression is true. So PowerShell is showing us EVERY Application EventLog entry in which the “Message” contains “iTunes” (case-insensitive).

  • Finally, notice in step 3 I wrapped the entire command in parenthesis ( and ) and then added the .UserName. That enabled me to hone in on just the specified property I was looking for, the user who installed iTunes. I could just have easily typed: Get-EventLog Application | ? {$_.Index -like "152"} and gotten back all of the properties, including UserName.
Advanced Aside: Although I ran this example against the event log on my local machine, I could just have easily used PowerShell on my local machine to check event logs on remote machines. It is not necessary to remote desktop into a remote machine to check event logs using PowerShell. If you are interested, check-out these two PowerShell commands: Invoke-Command and New-PSSession.

PowerShell

Introduction

PowerShell has been around for over seven years now. While the basics are easy, the more useful functionality is not easy to learn. Therefore, I came to my own PowerShell self-discovery several months back and since then have used it for all kinds of tasks: storing my passwords, managing my personal computers to reduce my use of the mouse, configuring remote computers, provisioning cloud infrastructures (on AWS and Azure), writing deployment scripts, and coding my own PowerShell command line tools.

So what is PowerShell?

It is a scripting language that runs inside a shell on the Microsoft platform. Significantly, it differs from DOS and scripting languages on other platforms in that it is .Net based, NOT text-based. In other words, in other popular shells, all input and output is in the form of text. This means the text must be manipulated in order to be of use as the input to commands or the output from commands. In PowerShell, while text (strings) are certainly supported, input and output is represented as .Net objects. This means that input and output does not require text manipulation and the user inherits all of the power of objects.

A couple more definitions:

In general, a scripting language is best suited for writing small blocks of code. It is high level, meaning it greatly abstracts the underlying hardware, making scripting good for rapidly writing functional programs to perform very specific tasks, sometimes at the expense of performance or flexibility.

Advanced Aside: Note that PowerShell is Turing Compete.

A shell, or command line interface, is an interpreter that hosts a scripting language. In other words, rather than compiling code into binary, a shell accepts scripts on the fly and executes them into meaningful actions.

Microsoft decided to call PowerShell commands cmdlets (pronounced command-lets).  Importantly, by convention, cmdlets are verb-noun pairs.  Actual examples include:

  • Get-ChildItem
  • Set-Item
  • Invoke-Command
  • Out-Host

Starting PowerShell

Modern Windows ships with two PowerShell shells, which Microsoft calls “Hosts.”  However, vendors are able to provide additional shells:

  • PowerShell: The default host
  • PowerISE (Integrated Scripting environments): A multi-tab environment for writing scripts and performing basic debugging with break points (NOTE: scripts can actually be written in any text editor as well as the standard PowerShell host).

The easiest way to launch PowerShell is just to start it from your Windows Programs menu.

The Three Key Commands

The following three cmdlets are all you need to know to get started using PowerShell:

  1. Get-Help (or just type ‘help’): Provides basic and extended help and syntax for any given PowerShell cmdlet.  For example, at the PowerShell prompt, type: Get-Help Get-Help

    Now trying typing this: Get-Help Get-Help -Full Notice this time PowerShell includes examples in addition to the basic usage.

    Advanced Aside: adding -Full is an example of something PowerShell calls a switch.

    NOTE: the first time you run help you may be promoted to update the help content.  This is because PowerShell is able to reach out over the Internet to download help content.  In fact, the clean install may not even ship with the help files installed until you run this download for the first time.

  2. Get-Member: Lists the methods and properties available on an object. Remember, PowerShell input and output are .Net objects, not text. Therefore, the input and output have methods and properties, just like all .Net objects do. For example, type: 7 | Get-Member. PowerShell tells you that “7” is an int (System.Int32) and lists the methods a .Net developer would expect to see on ints, such as ToString and GetType.

    Advanced Aside: the first part of this command, “7 | ” is called a pipe and without getting to detailed tells PowerShell to apply the Get-Member command to the number 7.

  3. Get-Command: Lists every cmdlet and function currently available to call in the PowerShell host (for now, ignore the difference between a function and cmdlet and think of them as the same). You can also narrow down the available cmdlets to list. For example, if you think the verb in the verb-noun cmdlet pair starts with “Get” you can type Get-Command get-*

    Note that PowerShell is case insensitive, “get-*” is the same as “Get-*” or “GET-*”

    Advanced Aside: I was very careful to state that Get-Command by itself lists the cmdlets PowerShell currently knows about. That is because it is possible to add more cmdlets into the PowerShell host’s scope.

Wildcards

As I demonstrated above, PowerShell also accepts wildcards for parameter values and as part of its Intellisense, when you are selecting cmdlets in the host or ISE. Type for example Get-H* and then press <tab>. Watch how PowerShell cycles through the list of available cmdlets each time you press <tab>.

Advanced Aside: There are other ways of doing this too. My first instinct was to pipe the result of Get-Process to a where clause. But I haven’t shown where clauses in this post. Fortunately, there are many ways to do the same thing in PowerShell, though they may not all have the same performance characteristics if you are running long running scripts, especially on remote computers.

Pipes and More

By way of demonstrating another important feature, try typing this: Get-Service | More into your PowerShell host (note it may not work in the ISE). This lists all Windows services in your computer but only displays enough to fill the current screen (so you don’t need the scroll bar). To continue through the list, hit the
<space> bar. The | is called the pipe and applies the output of the stuff to the left of the pipe as input to the stuff to the right of the pipe. I showed another example of this earlier when I did this 7 | Get-Member.

Cmdlets vs Scripting

In this post I have only demonstrated typing simple cmdlets or functions on the host. But PowerShell provides a rich syntax for writing scripts with standard programming constructs like loops, decisions, Boolean logic, etc.

Basic Cmdlet Example

I want to end this post by showing how you might use your newly acquired skills to list all running processes on your computer that start with the letter “S.” Let’s say you suspect their might be an out of the box PowerShell cmdlet for this. You already know that all cmdlets follow a verb-noun pair. So you might think the cmdlet contains the word “process.” Therefore, type this in your PowerShell host: *process* and press <tab>. Keep tabbing through…. ah, Get-Process looks promising. Let’s see what it does. Type Get-Help Get-Process. Looks good! The resulting help even shows a syntax with the parameter – Name. And you should be thinking to yourself, “Adam said I can include wildcards in my parameter values.” So now you type Get-Process -Name "S*" and watch the magic happen!

Stay Tuned:

So I’ve come to the end of this, the first blog post I’ve ever written. As time rolls on, I expect to expound on more features of PowerShell. If you’re really desperate to learn it in depth, now, however, then I strongly recommend this book: Windows PowerShell Cookbook.