Equals or == operator

During interview also, this can be asked in many different ways like what’s the difference between Equals and ==, why to use Equals for reference types and is there any reference type for which == is preferred and so on.

Equals basically check the contents of the object while comparing equality of two objects but has to be defined in Equals method of that class else it will return false for reference types. On the other hand == checks referential integrity as well along with contents.

e. g. In following example, s and s1 are both string type but s2 is string array with all three s, s1 and s2 having same content as abc.

string s = "abc";
string s1 = "abc";

//s == s1 will return true
//s.Equals(s1) will return true

char[] s2 = ['a','b','c'];
object o = new strung(s2);
//o==s2 will return false
//o.Equals(s2) will return true;

The same behavior is for all primitive data types like int etc. along with any value type. But not with in case of REFERENCE types.
 For reference types equality checking always use Equals until there is any referential integrity need to be checked. The only exception is string type as shown above. Even for reference type equality Equals method need to be overridden in the class. Will discuss about importance of Equals method and why it needs to be overridden will be explained in separate article.

Delete vs Truncate

There is always a confusion about delete and truncate specially when this question comes suddenly in interview.

Most of the time,  the simple answer is that there are logs created in Delete and no logs in case of truncate. But that is also not fully rue. The correct answer is : In case of delete, as number of log entries are created as number of records being deleted means one log per record deleted. In case of truncate, one log entry is created for the whole truncate.

Some more differences along with above main one can be listed as :

  • One Log entry for the whole transaction and one log per record deleted in case of delete.
  • Truncate deletes all records, but you can put where clause in case of delete.
  • The Identity column counter value resets to 1 in case of Truncate, in case of delete it stays to the last inserted record
  • Delete can be rolled back, truncate cannot. In other words, truncate is committed transaction.

Difference between String and StringBuilder

Strings are Immutable means whenever you change any value of string variable, a new object is created in memory. That’s why where a significant number of strings modifications need to be done, StringBuilder should be used. Its present in System.TextBuilder namespace.

Now questions comes, why strings are Immutable or what are the benefits of immutability :

  • First, All Immutable instances are thread safe. Because on modifying their values, a new instance is created , So no Thread can actually modify it.
  • There will be no side-effect on passing an immutable types as parameter in a function, as if there is x = “abc” in the starting of function,  that doesn’t change in the body of the method, then x == "abc" at the end of the method.
  • Copying is fast and simple, to create a clone just return this. Since the copy can’t be changed anyway, pretending something is its own copy is safe.
  • You can use strings in Hash-table, key-value pairs collection for their key part.

Dependency Property

Dependency properties are properties backed by WPF engine. They use more sufficient storage and support for additional feature such as change notifications & property value inheritance.

These are also the basis for a number of key WPF features like animation, data binding and styles.

Dependency properties in the WPF library are always wrapped by ordinary .Net property procedures.

e.g. Margin for any container control like StackPanel etc.

Steps in Creation of Own Dependency Property : 

  • Define an object that represents your property. (Add Property keyword at the end of property name)
    e.g.
public static readonly DependencyProperty MarginProperty;
  • Register a dependency property
    e.g.
static FrameworkElement()
{
     FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata(new Thickness(),
                                                                                       FrameworkPropertyMetadataOptions.AffectsMeasure);MarginProperty = DependencyProperty.Register("Margin",typeof(Thickness),
                             typeof(FrameworkElement),metadata,new ValidateValueCallback(FrameworkElement.IsMarginValid));
}

Adding a Property Wrapper

public Thickness Margin
{
      set { SetValue(MarginProperty,value);}
     get { return (Thickness)GetValue(MarginProperty);}
}

ITypedList and PropertyDescriptor simplified

Most of the time during databinding, you need some columns which are not present in your datasource object or collection object. At that time with the help of PropertyDescriptor and ITypedList interface, any number of virtual properties can be created and same can be added as columns.

e.g.

class A
{
public int x { get; set; }
public string name { get; set; }
}

class B
{
public List<A> aObj { get; set; }
public string empName { get; set; }
}

So if we should have some collection of type class B but with columns as empName, name, x and other based on combination of any of these, ITypedList can be very useful.

Here is the ITypedList and PropertyDescriptor modified for the above structure defined.

public class CollectionB : List<B>, ITypedList
{
      private PropertyDescriptorCollection propertyDescriptors;

public void CalculatePropertyDescriptors()
{
// start out with the properties that are really in the MasterRecord
PropertyDescriptorCollection origProperties = TypeDescriptor.GetProperties(typeof(B));

ArrayList properties = new ArrayList();

//this will Add all proprties of B apart from of type A
foreach (PropertyDescriptor desc in origProperties)
       if (!typeof(A).IsAssignableFrom(desc.PropertyType))
properties.Add(desc);

foreach (var item in this[0].aObj)
{
properties.Add(new PropertySummaryDescriptor(item.name.ToString()));
}

// Last, convert the ArrayList back into a PropertyDescriptorCollection.
propertyDescriptors = new PropertyDescriptorCollection((PropertyDescriptor[])properties.ToArray(typeof(PropertyDescriptor)));
}

#region ITypedList Members

public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
{
return propertyDescriptors;
}

public string GetListName(PropertyDescriptor[] listAccessors)
{
      return "";//Can be left blank
}

#endregion

}

Here is the Property Descriptor :

public class PropertySummaryDescriptor : PropertyDescriptor
{

public PropertySummaryDescriptor(string name)
: base(name, null)
{
}

public override bool CanResetValue(object component)
{
return false;
}

public override Type ComponentType
{
get { return typeof(B); }
}

public override object GetValue(object component)
{

B pr = (B)component;
    int x = 0;

    foreach (A a in pr.aObj)
if (a.name == this.Name)
x = a.x;
return x;

}

public override bool IsReadOnly
{
get { return true; }
}

public override Type PropertyType
{
get { return typeof(int); }
}

public override void ResetValue(object component)
{

}

public override void SetValue(object component, object value)
{

}

public override bool ShouldSerializeValue(object component)
{
return true;
}
}