It is possible with C# 3.0 to add method into sealed class. For more details please visit following link
Search
Jul 16, 2010
Sep 22, 2008
How to get Property list only of Derived class?
Hi All,
Recently I seen one requirement on ASP.Net Forum, some one posted question about How to get property list of Derived class.
Generally if we want to get all the property of class we use GetProperties() method. Which gives you the PropertyInfo[]. Lets see with example. We have following class Hierarchy.
class A
{
public int Id { get; set; }
public virtual void PrintMe()
{
Console.WriteLine("Id : " + Id);
}
}
class B : A
{
public string Name { get; set; }
public string Address { get; set; }
public override void PrintMe()
{
base.PrintMe();
Console.WriteLine("Name : " + Name);
}
}
In A there is only one property and in B we have 2 more property, our goal to get only two property declared in class B. Lets check with GetProperties().
PropertyInfo[] properties = typeof(B).GetProperties(); Console.WriteLine(properties.Count()); //will print 3
You can see its printing 3, that means it consider properties which are in parent and child too. There is one overload of GetProperties() which has BindingFlags, but with that also we are not achieving our goal.
So here is the solution using LINQ.
var prop =
from
p in typeof(B).GetProperties()
where p.DeclaringType == typeof(B)
select p;
Console.WriteLine(prop.Count()); //Will print 2
We are filtering on DeclaringType; which should be typeof(B). You can see now its returning 2, that what we needed.
Sep 14, 2008
Why doesn't C# support multiple inheritance?
I was always searching for the answer of "Why doesn't C# support multiple inheritance?".
After some googling I found the answer from MSDN.
Please have a look at the link.
Aug 12, 2008
Difference between internal and protected internal
Generally what we assume that internal means the variable that can only accessed within the assembly [which is the collection of namespace and classes].
But there is slide different meaning when we add protected to an internal member. What is that? The difference is... if you created a member which is protected internal then you can get it outside the assembly, but condition that you derive that class. Its protected so if you derive that class then in child class you can access the protected internal member.
We can check it out with example. Lets have a look at the classes which are in same assembly.
namespace Understand
{
public class C : UnderstandInternal.Class1
{
public C()
{
base.intInternalProtected(); // can call anything
base.intInternal();
}
}
}
namespace UnderstandInternal
{
public class Class1
{
internal int intInternal() { return 1; }
internal protected int intInternalProtected() { return 3; }
}
public class Class3
{
Class1 c = new Class1();
public Class3()
{
c.intInternalProtected(); // can call anything
c.intInternal();
}
}
}
You can see there is no problem in accessing the variable, its working fine, now we will do same thing in different assembly. Lets look at the following classes.
namespace TestConsole
{
class InternalTest : Class1
{
public InternalTest()
{
base.intInternalProtected(); //your can use here
base.intInternal(); //Error: 'UnderstandInternal.Class1' does not contain a definition for 'intInternal'
}
}
class Program
{
static void Main(string[] args)
{
Class1 c = new Class1();
c.intInternalProtected(); //Error: 'UnderstandInternal.Class1.intInternalProtected()' is inaccessible due to its protection level
c.intInternal(); //Error: 'UnderstandInternal.Class1' does not contain a definition for 'intInternal' and no extension method 'intInternal'
}
}
}
Aug 9, 2008
Parent can hold reference of Child
We all know that A a = new B(); we learn this by heart from the day we start OOPS.
But there are lots many people who just do cramming lolz, I have one good example which helps you to get your mind set what is meaning of A a = new B();
We all know that using virtual function we can achive the golden word "Parent can hold reference of child", but its not just virtual function involved, there are also reusability.
Lets say you have one base class having 10 properties and you derived 2 more classes which individual has more then 5-5 properties.
So what I do? Should I create methods for each child class? What if I have more 2-3 class?? The answer is NO [I knew that you know it :)]
So what to do? How A a = new B(); will help full here?
Lets learn with example.
I am haivng A as parent class and B, C as child class.
class A
{
public int Id { get; set; }
public virtual void PrintMe()
{
Console.WriteLine("Id : " + Id);
}
}
class B : A
{
public string Name { get; set; }
public override void PrintMe()
{
base.PrintMe();
Console.WriteLine("Name : " + Name);
}
}
class C : A
{
public string Company { get; set; }
public override void PrintMe()
{
base.PrintMe();
Console.WriteLine("Company : " + Company);
}
}
Now the method call, here is the code to calling method PrintMe
A a = new B();
a.PrintMe(); //will call B method
a = new A();
a.PrintMe(); //Will call A method
a = new C();
a.PrintMe(); //Will call C method
Now our purpose, what is it? Yes we need to set properties and then call PrintMe method.
private static void CallMe(A a)
{
a.Id = 4;
if (a is C)
((C)a).Company = "My Company";
else if (a is B)
((B)a).Name = "My Name";
a.PrintMe();
}
Lets call method CallMe.
1. a = new B();
2. CallMe(a); //Will print Id and Name
3. a = new C();
4. CallMe(a); //Will print Id and Company
So, this is the real world use of A a = new B();
Hope you understand, any question any query please feel free to approch me.
Jul 15, 2008
Overloading ToString Method of Enum
The general scenario for using enum is to provide a way to restrict a variable to one of a fixed set of values.
Now at the same time we need the string value of enum, and by default we get what we right as the name of enum, like I created following enum
public enum Operation
{
ChangePassword,
EditProfile,
SearchResult,
Search
}
Now when I try to call ToString method will show the name of enum.
Operation operation = Operation.ChangePassword;
Console.Write(operation.ToString());
//It will show you
//ChangePassword
Now what if I want to show Change Password insted of ChangePassword?
We can do it by Extension methods which are introduced in C# 3.0, I created following Extension method which operates on my Operation enum. Here is the method.
You see the switch case? I write the case for value which needs to show differently then name.
public static string EnumToString(this Operation operation)
{
switch (operation)
{
case Operation.ChangePassword:
return "Change Password";
case Operation.EditProfile:
return "Edit Profile";
case Operation.SearchResultL:
return "Search Result";
default :
return operation.ToString();
}
}
Operation operation = Operation.ChangePassword;
Console.Write(operation.EnumToString());
//this will show you what you needed
//Change Password
Jun 20, 2007
Design Guidelines for Class Library Developers
These guidelines are intended to help class library designers understand the trade-offs between different solutions. There might be situations where good library design requires that you violate these design guidelines. Such cases should be rare, and it is important that you provide a solid justification for your decision. The section provides naming and usage guidelines for types in the .NET Framework as well as guidelines for implementing common design patterns.
Read more: I have delicious Design Guidelines for Class Library Developers.
Mar 2, 2007
Overload property in .NET
I have new information to .NET programmers.
We can create or rather I say we can overload the property in VB.NET and C#
But there is a very vast difference in both languages
· VB.NET
Private ReadOnly Property MA() As String
Get
...
End Get
End Property
Private ReadOnly Property MA(ByVal movAvg As String) As String
Get
...
End Get
End Property
Ohh this is working like function overloading! But still its property!
· C#
public string Author
{
set {_author = value;}
get {return _author;}
}
public AuthorEntity Author
{
set {_authorEntity = value;}
get {return _authorEntity;}
}
You will be attempting to overload a function which differs only by return type, and it won't compile. this is why properties can't be overloaded in the way you want - because they are converted to get functions which would only differ by return type.
You can see the difference in both language. And its working!!!
Feb 28, 2007
Operator Overloading in VB.NET
We can overload the
I got good link having example how to overload the Operator in VB.Net.
Have a look at this delicious.
Feb 16, 2007
Static holder types should not have constructors [Microsoft.Design]
public class ServerController
{
public static ArrayList GetHttpServers() { ... }
public static ArrayList GetXmlRpcServer() { ... }
}
But as fas as performance concern this is wrong!! Why?
Warning : Remove the public constructors from 'ServerController'.
But i havnt declare any constructor!!
I can explain you, the default constructor is created automatically so how to avoid this message.
Two ways, 1) Make default constuctor as private or 2) Or make your class static.
Now look at class again
public static class ServerController
{
public static ArrayList GetHttpServers() { ... }
public static ArrayList GetXmlRpcServer() { ... }
}
Simple right?
I also have delicious on Microsoft Design
Feb 15, 2007
Compare two objects!!!
Use IComparer Generic Interface.
This interface provides
public int Compare(object x, object y)
you overiddes this method and implement yours, so simple right?
Example:
A delicious related to this topic!
public class MyComparer : IComparer
{
public MyComparer() { }
public int Compare(object x, object y)
{
return DateTime.Compare(((MyClass)x).EntryDate, ((MyClass)y).EntryDate);
}
}