Home arrow C# arrow Page 8 - C# - An Introduction
C#

C# - An Introduction


Learn the fundamentals of C# as Michael takes us on an introductory tour, including keywords, identifiers, and much more.

Author Info:
By: Michael Youssef
Rating: 4 stars4 stars4 stars4 stars4 stars / 310
November 17, 2003
TABLE OF CONTENTS:
  1. · C# - An Introduction
  2. · Keywords
  3. · Identifiers
  4. · C# Statements
  5. · C# Building-Blocks
  6. · Commenting Your Code
  7. · Case-Sensitivity and Syntax Errors
  8. · Organizing Code Using White Space
  9. · Conclusion

print this article
SEARCH DEVARTICLES

C# - An Introduction - Organizing Code Using White Space
(Page 8 of 9 )

To begin this section, let’s look at the following examples of a block of code:
 
{
    int x = 2    +    5 ; int y = x + 5;
    Console.WriteLine(x); Console.WriteLine(y);
}

Now look at this one:

{
    int x = 2 + 5;
    int y = x + 5;
    Console.WriteLine(x);
    Console.WriteLine(y);
}

Both of these blocks will run and return the same result.  The only difference between them is the white space inside our code.

White space is any “white space” inside your code (ie: space characters, tab characters, and carriage return characters).  Programmers use white space to make their code more readable and organized.  In the 2 blocks above, I used white space to write them but let’s see the first one again:

{
    int x = 2    +    5 ; int y = x + 5;
    Console.WriteLine(x); Console.WriteLine(y);
}

Here there are multiple spaces between “2”, “+” and “5”.   Also, I wrote two statements on the second line.

Now let’s look at the second block again:

{
    int x = 2 + 5;
    int y = x + 5;
    Console.WriteLine(x);
    Console.WriteLine(y);
}

It’s an organized block of code, containing four statements.  There’s a single line for each. 
Use white space to organize your code and make it more readable for you and for other developers.

We said that white space is spaces, tabs, and carriage returns characters.  If you look at the above blocks of code you will find all of them.  We used the tab character after the opening curly brace ( } ) to indent the body of the block.  We then used the Enter key (carriage return) to move to the next line after writing the first statement.  We used the space character between the operators (=, +) and numbers (2, 5) to make our code readable.

As we said before,  the C# compiler ignores comments.  It also ignores white space, which is why both blocks will return the same result.  If you wanted, you could also write the next block in two different ways:

{int x = 2 + 5;}

Or you can write it as the following:

{
    int x = 2 + 5;
}

By convention, indent your block’s body one tab character and take a single space character between operators like (=, +, -, *) and numbers.  Also, use a single carriage return character between statements.

VS.Net will help you indent your blocks.  If you’d like to see how it works, do the following in VS.Net:

  • Create a console application project and you will see the auto-generated code like this:

using System;
namespace testing
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //
                       // TODO: Add code to start application here
           //
        }
   }
}

If you look at this code you will see that it’s well-organized using the white space we discussed earlier; especially the tab character.

  • Write the following line of code:

static void Michael()

And press Enter.   Also, note here that VS.Net will not begin at the first column of the next line; rather, it will begin under the static keyword.

  • Type the opening curly brace “{“and press the Enter key. VS.Net will indent the body of the block (one tab character) and will automatically indent your cursor one tab:

static void Michael()
{
    |

As we said, VS.Net will help you indent your blocks, which will save you time trying to organize your blocks. The same thing happens if you have two or more nested blocks of code.  Look at the following block:

{
    int x = 5;
    if(x == 5)
    {
   Console.WriteLine(x);
    }
}

This is a block which is contains another block of code. Note that the first statement in the outer block is indented one tab and the inner block is indented one tab too but the statement inside the inner block is indented an extra tab.


blog comments powered by Disqus
C# ARTICLES

- Introduction to Objects and Classes in C#, P...
- Visual C#.NET, Part 1: Introduction to Progr...
- C# - An Introduction
- Hotmail Exposed: Access Hotmail using C#
- Razor Sharp C#
- Introduction to Objects and Classes in C#
- Making Your Code CLS Compliant
- Programming with MySQL and .NET Technologies
- Socket Programming in C# - Part II
- Socket Programming in C# - Part I
- Creational Patterns in C#
- Type Conversions
- Creating Custom Delegates and Events in C#
- Inheritance and Polymorphism
- Understanding Properties in C#

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials