18 February 2014

What is the difference between static and not static method in OOP

  • The main difference between static and non static method is Static method does not require an instance where non static method requires an instance to access them.
  • Example code in C#


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Calculator 
{
  //Static method 

  public static int sum(int a, int b)
   {
     return a + b;
   }
  //Non-Static methods

  public int subtract(int a, int b)
  {
     return a - b;
  } 

  public int Multilpy(int a, int b)
  {
    return a * b;
  }

  public int division(int a, int b)
  {
    return a / b;
  }
}


  • The sum method is declared as static, we can access the sum method without creating an instance of the Calculator class Code:


1
2
3
4
5
6
int firstNumber =int.Parse(textBox1.Text);
int secondNumber = int.Parse(textBox2.Text);

int sum = Calculator.sum(firstNumber, secondNumber);

MessageBox.Show("Result form Static Method sum="+sum);


  • Here,Method subtract is declared without the static keyword , that’s why is will act as non static, so if we want to access the subtract method of calculator class, then we have to create an instance of the calculator class first, then all the non-static methods will be accessible using the instance (i.e result1, result2) Code:


1
2
3
Calculator result1 = new Calculator(); 

MessageBox.Show("Result form Non Static Method subtraction= " + result1.subtract(firstNumber, secondNumber));

C 6.0 and the .NET 4.6 Framework

04 February 2014

Write your first "Hello World" Program in Visual C#


If you are new to C# programming language, then this blog post will help you to write your first C# program. I used "Microsoft Visual Studio 2010" to write this first "Hello World" program.

So,Installed the "Microsoft Visual Studio" on your PC and follow the step by step instruction given bellow:

Step 1: Start Visual Studio 2010

Start Visual Studio 2010
Start Visual Studio 2010

Step 2:. Click on New Project or Ctrl+Shift+N to create new project


Step 3: Click on Console Application. You may change the Name, Location and Solution Name

C# Console application
C# Console application


Step 4: Write the code inside the main function.

   Console.WriteLine("Hello World ! My first C# program.");

Hello World programming code in C#
Hello World programming code in C#

Step 5:. You first C# program is done.

Step 6:. Now run to program to see the output.
Click Debug ->Start without debugging or press ctrl+F5

Step 7:. The result will be show on a dos screen:

C# Hello World programming  output
C# Hello World programming  output

Step 8:. Enjoy programming with C#. Now you may write as may text as you want using the C# programming language. Head First C: A Learner's Guide to Real-World Programming with C#, XAML, and .NET

Featured Post

How to Write PHP code and HTML code within a same file

A PHP file can have both PHP code and HTML code together. Any HTML code written outside of PHP <?php //php code here… ?> Code is ig...

Popular Posts