What is the difference between typeof(), GetType() and IS in C# and VB.NET

What is the difference between typeof(), GetType() and is?

  • typeof() takes a type name specified at compile Time.
  • GetType() gets the Type of an Object at runtime
  • is returns true if an instance is in the inheritance tree. Since the is Keyword is casting the object, you should use it with care.
  • here is an example.

    class Employee { } 
    class Developer : Employee { }
    
    void PrintTypes(Employee e) { 
        print(e.GetType() == typeof(Employee)) // false 
        print(e is Employee)                   // true 
        print(e.GetType() == typeof(Developer))    // true
    }
    

    Please rate this snippet

    How to check if an array contains a value in Javascript

    To check if an array contains a value in Javascript you can use the snippet below.

    Sample Javascript

    Array.prototype.inArray = function (inputValue) {
    	var counter;
    	for (counter=0; counter < this.length; counter++) {
    		if (this[counter] === inputValue) {
    			return true;
    		}
    	}
    	return false;
    };
    

    Please rate this snippet

    How to disable comments on posts older than date X in WordPress

    To disable comments on posts older than date X in WordPress you can use the snippet below.

    Sample mySQL

    UPDATE wp_posts SET comment_status = 'closed' WHERE post_date < '2013-01-01' AND post_status = 'publish'
    

    Please rate this snippet

    How to remove any post that is older than X days in WordPress

    To remove any post that is older than X days in WordPress you can use the query below.

    Sample mySQL

    DELETE FROM wp_posts WHERE post_type = 'post' AND DATEDIFF(NOW(), post_date) > X
    

    Please rate this snippet

    How to get XNode as Xml String with indentation in C# and VB.NET

    Sample C#

    public static String XNodeToString(XNode node, int indentation = 2)
    {
    	using (var sw = new StringWriter())
    	{
    		using (var xw = new XmlTextWriter(sw))
    		{
    			xw.Formatting = Formatting.Indented;
    			xw.Indentation = indentation;
    			node.WriteTo(xw);
    		}
    		return sw.ToString();
    	}
    }
    

    Sample VB.NET

    Public Shared Function XNodeToString(node As XNode, Optional indentation As Integer = 2) As String
    	Using sw = New StringWriter()
    		Using xw = New XmlTextWriter(sw)
    			xw.Formatting = Formatting.Indented
    			xw.Indentation = indentation
    			node.WriteTo(xw)
    		End Using
    		Return sw.ToString()
    	End Using
    End Function
    

    for more informations on indentation = 2 see XmlTextWriter.Indentation Property

    Please rate this snippet

    How to get XmlNode as Xml String with indentation in C# and VB.NET

    Sample C#

    public static String XmlNodeToString(XmlNode node, int indentation = 2)
    {
    	using (var sw = new StringWriter())
    	{
    		using (var xw = new XmlTextWriter(sw))
    		{
    			xw.Formatting = Formatting.Indented;
    			xw.Indentation = indentation;
    			node.WriteContentTo(xw);
    		}
    		return sw.ToString();
    	}
    }
    

    Sample VB.NET

    Public Shared Function XmlNodeToString(node As XmlNode, Optional indentation As Integer = 2) As String
    	Using sw = New StringWriter()
    		Using xw = New XmlTextWriter(sw)
    			xw.Formatting = Formatting.Indented
    			xw.Indentation = indentation
    			node.WriteContentTo(xw)
    		End Using
    		Return sw.ToString()
    	End Using
    End Function
    

    for more informations on indentation = 2 see XmlTextWriter.Indentation Property

    Please rate this snippet

    How to query a DataTable using LINQ in C# and VB.NET

    To query a DataTable using LINQ in C# and VB.NET you can use the snippet below.
    In order to use the snippet, you need to reference System.Data.DataSetExtensions.

    Sample C#

    var resultData = (from myDataRow in myDataTable.AsEnumerable() where myDataRow.Field<Int32>("Fieldname") == 1 select myRow);
    

    Sample VB.NET

    Dim resultData = (From myDataRow In myDataTable.AsEnumerable() Where myDataRow.Field(Of Int32)("Fieldname") = 1myRow)
    

    Please rate this snippet