Obtain the namespace for either a Class or an Object

This post is just a reminder for me.  It’s not anything complex.  I had to write some code today to get the namespace as a string for a specific class and thought I would put it somewhere for quick reference.   I will include an example of obtaining the namespace for an object that is initialized for completeness.

  1. Example C# code to obtain namespace for a specific Class:

    var theNamespace = typeof(MyClass).Namespace
  2. Example C# code to obtain the namespace for an initialized Object:

    var myObject = new MyClass();
    var theNamespace = myObject.GetType().Namespace;