Learning C# Part 2: Language Fundamentals

Written By: Kevin Jordan

- 07 Jul 2006 -
















Description: This series aims to take a beginning programmer to an intermediate level in C#. Topics covered in Part 2 include: C# program structure, declaring and intializing variables, characters, strings, constants, enumeration, converting between types, and expressions and operators.

  1. C# Program Structure
  2. Declaring and Using Different Variable Types
  3. Constants and Enumeration
  4. Converting Variables and Using Expressions
  5. Aww Man... Not Homework

Introduction

The goal of these articles is to take a beginning programmer to an intermediate level in C#. By the end of this series you should be able to use object oriented to techniques to create database or xml driven websites and Windows-based applications.

I think practice problems can provide a significant benefit rather than just reading material because that's where you realize if you really understand a concept or not. So, at the end of each section there will be a small practice. If you run into any problems along the way, feel free to , or post it on the forum.

C# Program Structure

Now that you've finished your first C# program, we should probably get a little more in depth about the structure of the language.

using System;
 
class myClass
{
        static void Main()
        {
                // This is a comment
                Console.WriteLine ("Write this to the console");
        }
}

The using keyword refers to namespaces and classes that you program will use as a resource. System is a namespace that provides the basic system functionality upon which this application is built. Namespaces are similar to the folder structure of your operating system in that it helps divide class libraries into a logical hierarchy.

Object oriented programming requires the use of classes which are used to create objects and methods. Methods describe the behavior of a class. Main() is a global method that tell the compiler where to start execution of an application. We'll dive a lot more into class, objects, and methods in a later section.

Console.WriteLine… is a statement. Statements are instructions used to perform a specific action. Statements require a semicolon to distinguish the end of the instruction. While you can include more that one statement on a line, it's considered bad programming practices.

Blocks of code are section off using braces, { }. The class and Main method both start with and opening brace and ends with a closing brace.

In order to make code more easily maintainable, it's a good idea to add comments. Single line comments start with //. Everything after the // is a comment that is ignored during compile time, but will definitely help when someone else is trying to understand your code. Multi-line comments start with /* and end with */. Here's an example:

/* This is a comment
even if I use multiple lines it will not
execute anything I type
Console.WriteLine("This will not be executed"); */
Console.WriteLine("This will be executed");

Next >>