Tag: oops

Compare 2 objects in c#

public static bool PublicInstancePropertiesEqual(T self, T to, params string[] ignore) where T : class
{
if (self != null && to != null)
{
Type type = typeof(T);
List ignoreList = new List(ignore);
foreach (System.Reflection.PropertyInfo pi in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
{
if (!ignoreList.Contains(pi.Name))
{
object selfValue = type.GetProperty(pi.Name).GetValue(self, null);
object toValue = type.GetProperty(pi.Name).GetValue(to, null);

if (selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue)))
{
return false;
}
}
}
return true;
}
return self == to;
}


Private Constructor and Uses

When you declare a Constructor with Private access modifier then it is called Private Constructor. We can use the private constructor in singleton pattern.

If you declare a Constructor as private then it doesn’t allow to create object for its derived class, i.e you loose inherent facility for that class.

Example:

Class A

{

// some code

Private Void A()

{

//Private Constructor

}

}

Class B:A

{

//code

}

B obj = new B();// will give Compilation Error


Default Access Specifiers

n enum has default modifier as public

A class has default modifiers as private . It can declare members (methods etc) with following access modifiers:
public
internal
private
protected internal

An interface has default modifier as public

A struct has default modifier as private and it can declare its members (methods etc) with following access modifiers:
public
internal
private

A methods, fields, and properties has default access modifier as “Private” if no modifier is specified.


Why use properties ?

Advantage of using Properties

What is Properties?

Attribute of object is called properties. Eg1:- A car has color as property.
Example of using properties in .Net:

private string m_Color;;

public string Color
{
get
{
return m_Color;
}
set
{
m_Color = value;
}
}

Car Maruti = new Car();
Maruti.Color= “White”;
Console.Write(Maruti.Color);


Why use Polymorphism

You would use it when you need a class that could be used in different
ways. For instance try creating a ‘Person’ interface and then implement that
interface in a class called ‘Man’ – implement another class the ‘Person’
interface and call this class ‘Women’ – Now both the ‘Man’ & ‘Women’ Classes
derive there members from the Person interface. The ‘Man’ is a ‘Person’ and
the ‘Women’ is a person. If we had created just two classes called ‘Man’ and
‘Women’ then they would have no connection and would also use most of the
same member names, even with the same logic. In a real time application you
would use it exactly the same way – You will need to plan your project with
polymorpism in mind, compare types, do they implement many of the same
features?


  • Cloud World

  • Custom Search



  • Cyberbrutus
    iDream theme by Templates Next | Powered by WordPress

    Switch to our mobile site