Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, July 28, 2014

Exploring Abstract class (OOP concepts) in .NET - My Observations

Following are my observations with Abstract classes in .NET.
Feel free to write in comments if you think of more points to be added in this list, or if you want know more details about one or more points :)

  1. A class must be declared with "abstract" keyword
  2. It can contain complete implantation of methods
  3. Methods declared with "abstract" keyword can contain only declarations in Abstract class
  4. We cannot create an instance of abstract class
  5. Abstract class must be inherited
  6. A class derived from an abstract class must contain complete implementation of all abstract methods
  7. An abstract class can be derived from another abstract class
  8. Abstract class can be used as a parameter in methods
  9. If a class contains an abstract method, then that class must be marked as "abstract"
  10. An abstract class can have a constructor
  11. An abstract class can be inherited from a non-abstract class
  12. Constructor rules are also applied in case of abstract classes inheritance

P.S. - Please note all keyword and code snippets (if demonstrated above) are in C#.NET 

Thursday, July 24, 2014

How constructors are called while the classes are inherited: Exploring Object Oriented Programming and Concepts

Constructor Calls in Inheritance

Case study 1:

ClassA is a base class of ClassB.
Both classes have default constructor.

Observations:
While creating a new instance of ClassB (without parameters):
- it will first call default constructor of ClassB which will call constructor of ClassA.
- it will complete executing code of default constructor of ClassA.
- after that, it will complete executing code of default constructor of ClassB.

Case study 2:

ClassA is a base class of ClassB.
ClassA has no default constructor, but only parameterized constructor.
ClassB has a default constructor.


Observations: 
- it will not allow creating ClassA without a default constructor 
- if you want to do so, you need to call a base class constructor from ClassB's constructor: 
For example, following is the constructor of class A 
public ClassA(string someVariable) ..... some code .... }  
To allow ClassA without a default constructor, its all child classes should call base class constructor as below: 
public ClassB(): base("some value") 
or
public ClassB(string fullName): base(fullName)

Tuesday, June 25, 2013

C# - Generate random number

Sometimes we need to generate a random number in our application for various purpose (such as, to use it as a part of unique key, etc).

There is a quick way in C#, that can generate a random number. Here, you can also specify the range within which the random number should be generated.

int generatedRandomNumber = new Random(System.DateTime.Now.Millisecond).Next(int.MinValue, int.MaxValue);

This will generate a random number within the range of -2147483648 and 2,147,483,647. But you can narrow down your range to only positive values or some explicit range by changing the parameters in Next function above.

Hope it does help!
 

Monday, May 21, 2012

Remove HTML tags from a text string in ASP.NET using RegularExpression

This article aims to explain a very simple method to remove HTML tags from a text using RegularExpressions.

This will remove all HTML tags, or character references (like, &nbsp, &amp) from a text, and will return plain text.

    public static string RemoveHtmlTags(string htmlText, bool preserveNewLine)
    {
        System.Web.UI.HtmlControls.HtmlGenericControl divNew = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
        divNew.InnerHtml = htmlText;
        if (preserveNewLine)
        {
            divNew.InnerHtml = divNew.InnerHtml.Replace("<br>", "\n");
            divNew.InnerHtml = divNew.InnerHtml.Replace("<br/>", "\n");
            divNew.InnerHtml = divNew.InnerHtml.Replace("<br />", "\n");
        }
        return System.Text.RegularExpressions.Regex.Replace(divNew.InnerText, "<[^>]*>", "");
    }

if there is a requirement to preserve new line, then we need to convert HTML line breaks into new line character (which is "\n" in C#).

Example output using above method:
Input: &nbsp;Take one <strong>notebook</strong>, pen, <u>pencil</u> and <em>eraser</em> with you.
Result:  Take one notebook, pen, pencil and eraser with you.
(In above example, &nbsp; is replaced by a space character in the beginning)