Monday, August 2, 2010

C# - Basics by Example



The first of a series of basics statements and constructs of .NET/JVM Programming Language is here! No boring step by step explanations... just code!
You can copy and paste the code below in your favorite IDE/Editor and start playing and learning with it. This little "working" program will teach you the basics of the Programming Language.

There are some "comments" on the code added just to tell you what are or how are some features called. In case you do not know the theory part, then you can read my previous post, where I give a definition of each of the concepts mentioned on the code. You can find it here: http://carlosqt.blogspot.com/2010/08/new-series-languages-basics-by-example.html 

Today's version of the Greetings Program is written in C# Enjoy!

Greetings Program - Verbose
// C# Basics
namespace CsGreetProgram
{
    using System;
    // Greeting Class
    internal class Greet  
    {  
        // Fields or Attributes
        private string message;
        private string name;
        private int loopMessage;
        // Properties
        public string Message
        {
            get { return this.message; }
            set { this.message = this.Capitalize(value); }
        }
        public string Name
        {
            get { return this.name; }
            set { this.name = this.Capitalize(value); }
        }
        public int LoopMessage
        {
            get { return this.loopMessage; }
            set { this.loopMessage = value; }
        }
        // Constructor
        public Greet() 
        {
            this.message = "";
            this.name = "";
            this.loopMessage = 0;
        }
        // Overloaded Constructor
        public Greet(string message, string name, int loopMessage)
        {
            this.message = this.Capitalize(message);
            this.name = this.Capitalize(name);
            this.loopMessage = loopMessage;
        } 
        // Method 1
        private string Capitalize(string val)
        {
            // "if-then-else" statement
            if (val.Length >= 1)
            {
                return val[0].ToString().ToUpper() + val.Substring(1, val.Length - 1);
            }
            else
            {
                return "";
            }
        }
        // Method 2
        public void Salute()  
        {
            // "for" statement
            for (int i = 0; i < this.loopMessage; i++)
            {
                Console.WriteLine("{0} {1}!", this.message, this.name);
            }
        }
        // Overloaded Method 2.1
        public void Salute(string message, string name, int loopMessage)
        {
            // "while" statement
            int i = 0;
            while(i < loopMessage)
            {
                Console.WriteLine("{0} {1}!", this.Capitalize(message), 
                                 this.Capitalize(name));
                i++;
            }
        }
        // Overloaded Method 2.2
        public void Salute(string name)
        {
            // "switch/case" statement
            DateTime dtNow = DateTime.Now;
            switch (dtNow.Hour)
            {
                case 6: case 7: case 8: case 9: case 10: case 11:
                    this.message = "good morning,";
                    break;
                case 12: case 13: case 14: case 15: case 16: case 17:
                    this.message = "good afternoon,";
                    break;
                case 18: case 19: case 20: case 21: case 22: 
                    this.message = "good evening,";
                    break;
                case 23: case 0: case 1: case 2: case 3: case 4: case 5: 
                    this.message = "good night,";
                    break;
                default:
                    this.message = "huh?";
                    break;
            }
            Console.WriteLine("{0} {1}!", this.Capitalize(this.message), 
                             this.Capitalize(name));
        }
    }
  
    // Console Program
    internal class Program  
    {                  
        public static void Main(string[] args)  
        {            
            // Define object of type Greet 
            Greet g;
            // Instantiate Greet. Call Constructor
            g = new Greet();
            // Call Set Properties
            g.Message = "hello";
            g.Name = "world";
            g.LoopMessage = 5;
            // Call Method 2
            g.Salute();
            // Call Overloaded Method 2.1 and Get Properties
            g.Salute(g.Message, "c#", g.LoopMessage);
            // Call Overloaded Method 2.2
            g.Salute("carlos");
            
            // Stop and exit
            Console.WriteLine("Press any key to exit...");
            Console.Read();
        }          
    }
}

Greetings Program - Minimal
// C# Basics
using System;
// Greeting Class
class Greet
{
    // Fields or Attributes
    string message, name;
    int loopMessage;
    // Properties
    public string Message
    {
        get { return message; }
        set { message = Capitalize(value); }
    }
    public string Name
    {
        get { return name; }
        set { name = Capitalize(value); }
    }
    public int LoopMessage
    {
        get { return loopMessage; }
        set { loopMessage = value; }
    }
    // Constructor
    public Greet()
    {
        message = "";
        name = "";
        loopMessage = 0;
    }
    // Overloaded Constructor
    public Greet(string message, string name, int loopMessage)
    {
        this.message = Capitalize(message);
        this.name = Capitalize(name);
        this.loopMessage = loopMessage;
    }
    // Method 1
    string Capitalize(string val)
    {
        // "if-then-else" statement
        if (val.Length >= 1)        
            return val[0].ToString().ToUpper() + val.Substring(1, val.Length - 1);
        else
            return "";
    }
    // Method 2
    public void Salute()
    {
        // "for" statement
        for (int i = 0; i < loopMessage; i++)
            Console.WriteLine("{0} {1}!", message, name);
    }
    // Overloaded Method 2.1
    public void Salute(string message, string name, int loopMessage)
    {
        // "while" statement
        int i = 0;
        while (i < loopMessage)
        {
            Console.WriteLine("{0} {1}!", Capitalize(message), 
                             Capitalize(name));
            i++;
        }
    }
    // Overloaded Method 2.2
    public void Salute(string name)
    {
        // "switch/case" statement
        DateTime dtNow = DateTime.Now;
        switch (dtNow.Hour)
        {
            case 6: case 7: case 8: case 9: case 10: case 11:
                message = "good morning,";
                break;
            case 12: case 13: case 14: case 15: case 16: case 17:
                message = "good afternoon,";
                break;
            case 18: case 19: case 20: case 21: case 22: 
                message = "good evening,";
                break;
            case 23: case 0: case 1: case 2: case 3: case 4: case 5: 
                message = "good night,";
                break;
            default:
                message = "huh?";
                break;
        }
        Console.WriteLine("{0} {1}!", Capitalize(message), 
                         Capitalize(name));
    }
}

// Console Program
class Program
{
    static void Main(string[] args)
    {
        // Define and Instantiate object of type Greet. Call Constructor
        Greet g = new Greet();              
        // Call Set Properties
        g.Message = "hello";
        g.Name = "world";
        g.LoopMessage = 5;
        // Call Method 2
        g.Salute();
        // Call Overloaded Method 2.1 and Get Properties
        g.Salute(g.Message, "c#", g.LoopMessage);
        // Call Overloaded Method 2.2
        g.Salute("carlos");        

        // Stop and exit
        Console.WriteLine("Press any key to exit...");
        Console.Read();
    }
}

And the Output is:

















Auto-Implemented Properties in C#

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors. Taken from: http://msdn.microsoft.com/en-us/library/bb384054.aspx


/* That means that we can omit the Fields/Attributes declaration
   and go directly to the properties  */
        // private string message;
        // private string name;
        // private int loopMessage;
        // Auto-Implemented Properties
        public string Message
        {
            get; set;
        }
        public string Name
        {
            get; set;
        }
        public int LoopMessage
        {
            get; set;
        }

/* then, when ever you want to use them you get and set their value using the properties only/ Let's see an example in our constructor */
        // Constructor
        public Greet()
        {
        // error: Greet does not contain a definition for message/name/loopMessage
         // this.message = "";  
         // this.name = "";
         // this.loopMessage = 0;
         // We need to use the properties: 
            this.Message = "";  
            this.Name = "";
            this.LoopMessage = 0;
        }

No comments:

Post a Comment