C#
  Home arrow C# arrow C# Simplified, part 2: Methods & Programmi...
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

C# Simplified, part 2: Methods & Programming Constructs
By: Anand Narayanaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 16
    2005-05-10

    Table of Contents:
  • C# Simplified, part 2: Methods & Programming Constructs
  • Declaring methods with parameters
  • Overloading Methods
  • Different types of programming constructs in C#
  • Different types of looping statements in C#
  • break Statement

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    C# Simplified, part 2: Methods & Programming Constructs


    (Page 1 of 6 )

    In this article, you will learn about methods, which perform actions and carry out funtions. You will also learn about programming constructs and looping statements in C#. This is the second article in a series covering C#; the first article in the series is available here.

    About the C# Simplified Series

    C# Simplified covers each and every concept of the C# programming language in a concise manner. The articles in this series have been divided into several parts and will provide detailed explanations along with source code and screenshots. This series has been specifically written for beginners and students with an aim to teach C# in the quickest possible time. Please send your comments to csharpsimplified@gmail.com.

    Methods

    Methods are building blocks in C#. They perform some kind of action or carry out many functions such as printing, saving and opening a dialog box. The action could be anything, based on your application. For instance, the code for printing a report can be given inside a method. After declaring the method you can call it, preferably with a button event. You can also reuse this method with the help of inheritance. Inheritance is one of the features of any Object Oriented programming language such as C#.

    There are basically two types of methods in C#. They are known as instance and class methods. Even though both methods perform the same work, there are differences in their declaration and usage. Methods can also be declared with parameters, and accessed by passing the required values.

    Instance Method

    The instance method is a method which is declared outside the Main() method. It can be accessed only by creating an object of the class, as in the case of an Instance Variable (see Listing 2.1).

    Listing 2.1

    using System;

    class Instancemethod

    {

    //Method declared outside the main and hence Instance Method.

    void show()

    {

    int x = 500;

    int y = 700;

    Console.WriteLine(x);

    Console.WriteLine(y);

    }

    public static void Main()

    {

    //Object created

    Instancemethod IM = new Instancemethod();

    //Instance method called

    IM.show();

    }

    }

    In the above listing, a method named show() is declared, which prints the values of two variables. Inside the Main() method, an object of the class named IM is created and the method is called.

    Class Method

    A class method is also placed outside the Main() method with the exception that it should be declared with the keyword static. An interesting point to note is that class methods can be accessed without creating an object of the corresponding class. The general format for accessing a class method is C lassname.Methodname. Listing 2.2 illustrates the working of a class method:

    Listing 2.2

    using System;

    class Classmethod

    {

    //A class method declared, notice the static keyword

    static void show()

    {

    int x = 700;

    int y = 900;

    Console.WriteLine(x);

    Console.WriteLine(y);

    }

    public static void Main()

    {

    /* Class method called without creating an object

    of the class */

    Classmethod.show();

    }

    }

    In the above listing, a method named show() is declared, which prints the values of two variables. Inside the Main() method, the method is called without creating an object of the class. This is because of the static keyword, which we applied while declaring the method.

    More C# Articles
    More By Anand Narayanaswamy


     

    C# ARTICLES

    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained
    - C# FileStream Explained

    Iron Speed




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway