Inheritance Simply

Basics are important. Involving in big projects may make us underestimate and forget about the basics. While I was rechecking the important feature of OOP: inheritance, I wanted to share very simple example briefly.

My little piece of code here calculates the surface and volume of a rectangular prism and calculates its cost based on its volume.

Firtly, I created a base class which is called RectangularPrism, added three number we need for calculation: length,width,height, created the public constructor and then created the methods for calculating volume, surface and displaying the all features of the rectangular prism.

class RectangularPrism
protected double length;
protected double width;
protected double height;

public RectangularPrism(double l,double w,double h)
length = l;
width = w;
height = h;
public double GetSurface()
return (2 * ((length * width) +(length * height) +(width * height)));

public double GetVolume()
return length * width * height;
public void Display()
Console.WriteLine("Length: {0}",length);
Console.WriteLine("Width: {0}",width);
Console.WriteLine("Height: {0}",height);
Console.WriteLine("Surface: {0}", GetSurface());
Console.WriteLine("Volume: {0}", GetVolume());

Next, I created another class called Box inherited from RectangularPrism. Additional to the features of base class, Box class has a method to calculate the cost according to its volume. (Volume x 2 is my formula for calculating the cost). It has also a Display() method which runs base.Display and runs an additional Console.Write method.

class Box : RectangularPrism
public Box(double l, double w, double h) : base(l,w,h)
{ }

public double GetCost()
double cost;
cost = GetVolume() * 2;
return cost;


public new void Display()
Console.WriteLine("Cost: {0}", GetCost());

Lastly, we can use our classes and create and object in our main method of the class.

class Program
static void Main(string[] args)
Box myBox = new Box(8,4,4);


With those numbers, it will give us following result:


Inheritance, together with encapsulation and polymorphism, is one of the three primary characteristics (or pillars) of object-oriented programming. Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes.

For more information from msdn >>

#c# #inheritance

Simple XML Reading,Writing and Serialization

Altough JSON became more common, XML is still an important markup language. As a snippet, I created an example project which will be creating a Books.xml file and reading it from the file.

First of all, let’s create a class called Book with parameters of book name, writer and list of readers.

 public class Book 
 private string bookName;
 public string BookName 
 get { return bookName; }
 set { bookName = value; }

 private string bookWriter;
 public string BookWriter
 get { return bookWriter; }
 set { bookWriter = value; }

 private List<string> bookReaders = new List<string>();
 public List<string> BookReaders 
 get { return bookReaders; }
 set { bookReaders = value; }

Continue reading

#class-to-xml-c, #xml, #xml-serialization

Basics of LINQ

Some notes on LINQ
Language-Integrated Query (LINQ) is the technology that provides tools for developers to use query directly in the programming language (C#). A query is an expression that retrieves data from a data source. In LINQ, you work with objects. You use the codes to query and transform data in XML documents, SQL databases, Datasets, collections, and any other format that LINQ is available.

All LINQ operations include there actions:

  1. Obtain the data source.
  2. Create the query.
  3. Execute the query.

A simple example of LINQ:

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

var queryLowNums =
from num in numbers
where num &lt; 6
select num;

foreach (int num in numQuery)
Console.Write("{0,1} ", num);

This is just an introduction to LINQ. I will share more while I build with it.


Thread was being aborted, Response.Redirect

Sometimes, trying to handle exceptions with a statement that redirects the page, we may get a ThreadAbortException error.

“If you use the Response.End, Response.Redirect, or Server.Transfer method, a ThreadAbortException exception occurs. You can use a try-catch statement to catch this exception.”

The solution is using these two lines:


Oficilal solution page from Microsoft:

Check if string contains only digits

  • Fastest way to check if string contains only digits

    bool IsDigitsOnly(string str)
        foreach (char c in str)
            if (c < '0' || c > '9')
                return false;
        return true;

  • Difference between Char.IsDigit() and Char.IsNumber() in C#

Convert an oracle BLOB image (Byte[]) to base64 for web service

while (reader.Read()) {
               PhotoInfos myPhoto = new PhotoInfos();
               byte[] image = (byte[])reader[“image”];
               int imageLength = image.Length;
               string base64image = Convert.ToBase64String(image, 0, imageLength);
      = base64image;

Remove the last character of a StringBuilder object

Updating data depending on the whether or not the parameters are set may cause various problems. For example,

StringBuilder sqlCommand = new StringBuilder();
                sqlCommand(@"update myTable set ");

                    sqlKomut.Append(" id=:ID, ");
                    command.Parameters.AddWithValue(":ID", parameters.ID);
                    sqlKomut.Append(" name= :NAME, ");
                    command.Parameters.AddWithValue(":NAME", parameters.NAME);

In this case, if you send only ID to set, as it will be the last parameter in the query, it may cause an  error. By removing last comma, you can add as many parameters as you want.

                //Removing commas
                sqlCommand = new StringBuilder(sqlKomut.ToString().Trim().TrimEnd(‘,’));