Wednesday, June 15, 2011

Difference between Class and Structures in C#

Hi! It's me again, Eric and now i would to differentiate classes and structures in C#.
So let us define what are classes and structures first. Class are set of data that can be inherit. They can be declared using the keyword class. They are surrounded by curly braces and each of it have constructors. On the other hand, Structures are quite similar to classes but have some restriction.

Differences:
a.) Classes can be inherited, while Structures cannot.
b.) You can initialize objects in class, while on structures you either use a constructor in initializing variables.
c.) Structure can't have a default parameter-less constructor or destructor.

Have a look at my sample:

using System;


namespace StructureVsClassConsoleBlog
{
    // Class
    class Program
    {
        int intVariable;
        int intVariable2 = 20; // You can initialize a value here.


        public Program()
        {
            this.intVariable = 28; // My birthday! :@)
        }


        public static void Main()
        {
            // Calling the class.
            Program program = new Program();
            // Calling the structure.
            SampleStructure structure = new SampleStructure(14);
            Console.WriteLine(structure.intStructVariable);
            structure.intStructVariable = 1;
            Console.WriteLine(program.intVariable);
            Console.WriteLine(structure.intStructVariable);
            structure.StructureMethod();


            Console.ReadKey();
        }
    }


    // Structures
    struct SampleStructure
    {
        public int intStructVariable; // Cannot be initialize here.


        public SampleStructure(int number)
        {
            this.intStructVariable = number;
        }


        public void StructureMethod()
        {
            Console.WriteLine("This is inside the StructureMethod().");
        }
    }
}


Result:
14                                                               
28                                                              
1                                                                 
This is inside the StructureMethod(). 

Sorry guys, i'm a little bit tired in changing the colors of my code.
Have fun! :@)

Learning Overloading and Overriding in C#

So let us start with overloading.
What is overloading? overloading is a feature of the object-oriented concepts called "Polymorphism". It has the ability to create multiple functions/methods or properties with the exact name but different parameters. To make it more clearer, take a look at my example.

using System;


namespace ConsoleApplicationForBlog
{
    class Program 
    {
        int firstNumber;
        int secondNumber;
        
        //
        // Overloading
        //
        private int Add(int x, int y)
        {
            return (x + y);
        }


        private string Add(string x, string y)
        {
            return (x + y);
        }


        static void Main(string[] args)
        {
            Program program = new Program();
            // For integer
            int result;
            
            program.firstNumber = 10;
            program.secondNumber = 15;


            result = program.Add(program.firstNumber, program.secondNumber);
            Console.WriteLine(result);
            
            // For string
            string fullName;


            fullName = program.Add("Eric John", " Adamos");
            Console.WriteLine(fullName);
            Console.ReadKey();
        }
    }
}


The result is:
25                        
Eric John Adamos  

Overriding is another feature of polymorphism in which such cases that you would want to change the functionality of that method for another class without changing its base functionality. In this case, we can override the property of the base class. Take a look at my code:


using System;

namespace OverridingConsoleBlog
{
    // This is your base class.
    public class Animal
    {
        public virtual void display()
        {
            Console.WriteLine("Animal!");
        }
    }

    // This is a class named "Dog" which inherits "Animal".
    public class Dog : Animal
    {
        // Note: the name of your method should be
        // the same on your base class.
        public override void display()
        {
            // This overrides your base class display().
            Console.WriteLine("Hi! I'm a dog.");
        }
    }

    public class Cat : Animal
    {
        // Note: the name of your method should be
        // the same on your base class.
        public override void display()
        {
            // This overrides your base class display().
            Console.WriteLine("Hi! I'm a cat.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Animal animal = new Animal();
            Animal dog = new Dog(); // or Dog dog = new Dog();
            Animal cat = new Cat(); // or Cat cat = new Cat();

            animal.display(); 
            dog.display();
            cat.display();

            Console.ReadKey();
        }
    }
}

Result :
Animal!           
Hi! I'm a dog.  
Hi! I'm a cat.   

Have fun! :)

Wednesday, June 8, 2011

Learning String.Compare Method in C#

Hi, i would like to talk about the use of String.Compare function.

So what is String.Compare()? It is a built-in function in C# that used to compare 2 strings.

Take a look at its parameter:
String.Compare(string strA, string strB, bool ignoreCase)

strA
          This is your first string.
strB
          This is your second string.
ignoreCase
          Mark true if you want to ignore the casing, false if otherwise.

So speaking of its advantage, for example, comparing string in an if-else statement.

struct StringCompareExample
{
     static void Main(string[] args)
     {
          string strA = "Eric";
          // instead of using this kind of comparison
          if(strA == "Eric".ToUpper())
          {
               Console.Writeline("Equal");
          }

          else
          {
               Console.Writeline("Not Equal");
          }

          // use this
          // Noticed that, it can also be useful if you are trying to ignore cases
          // between strings.
          if(!Convert.ToBoolean(String.Compare(strA, strB, true)))
          {
               Console.Writeline("Equal");
          }
          else
          {
               Console.Writeline("Not Equal");
          }

          Console.ReadKey();
     }
}

Tuesday, May 31, 2011

A new beginning

Hi Everyone!

This is my first post since i just realize how important blogging is.
Well, wait for my next post. Good Day everyone.

Eric :)