My Tutorials: July 2012

Tuesday, 31 July 2012

Integrated Development Environment (IDE)

An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of:
  1. a source code editor
  2. build automation tools
  3. a debugger
Depending on what language you are programming in you will have to install the correct IDE so that you can have access to the correct libraries and you can build your solution to run on the target system.

For more information please refer to the wikipedia entry.

Installing Microsoft Visual Studio C# Express

Before I just dive into programming in C# I would be unkind to just assume everyone has the correct IDE available and installed for them to use. So for those of you who are completely new to this process here is a quick how-to on what you will be needing to get yourself programming in C#. For those of you that have already done this please be patient.

First you will need to go and download the Microsoft Visual Studio C# Express package at :

Download package

I may have to add my own link to this at a later stage if Microsoft ever decides to remove this download link, but that is a concern for a later stage. If you do come across download issues please let me know.

Once you have downloaded the package you will need to install as you would a normal program. Microsoft has kindly made this light weight IDE available free of charge with the main purpose of making their tools available for students who would otherwise not be able to afford the tool.

When you have your development environment up and running you will be able to program to your hearts content. I will attempt to provide a very progressive blog so that you will be able to learn from the basics to more advanced concepts.

If you have any issues please let me know and I will try elaborate the install process a little bit more.

C# Value Keyword

The contextual keyword value is used in the set accessor in ordinary property declarations. It is similar to an input parameter on a method. The word value references the value that client code is attempting to assign to the property. In the following example, MyDerivedClass has a property called Name that uses the value parameter to assign a new string to the backing field name. From the point of view of client code, the operation is written as a simple assignment.

The MSDN reference can be found here.

Code Example


 class MyBaseClass
        {
            // virtual auto-implemented property. Overrides can only
            // provide specialized behavior if they implement get and set accessors.
            public virtual string Name { get; set; }

            // ordinary virtual property with backing field
            private int num;
            public virtual int Number
            {
                get { return num; }
                set { num = value; }
            }
        }

        class MyDerivedClass : MyBaseClass
        {
            private string name;

            // Override auto-implemented property with ordinary property
            // to provide specialized accessor behavior.
            public override string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    if (value != String.Empty)
                    {
                        name = value;
                    }
                    else
                    {
                        name = "Unknown";
                    }
                }
            }
        }


As seen in the above code, the value keyword gives us access to the implied value that is sent when the Name property is assigned a value. This allows us to do error handling before we assign the value to our name field.

C# Yield Keyword

The yield return and yield break keywords are shortcuts introduced in C# 3.0. They are designed to assist you by removing verbose code in methods that return IEnumerable types (i.e. IEnumerable <T>).

You can find the MSDN reference here.

Old Methodology

Usually a method that returns a collection consists of the following steps:
  1. Create an empty IEnumerable set. 
  2. Loop through some data (via a while, foreach or other construct) 
  3. Add to the set within your loop. 
  4. Exit the loop when some limit is reached. 
  5. Return the set 
Below is an example of the process required.

public IEnumerable<int> GetEvenNumbers(int maxCount)
        {
            //create and initialise IEnumerable container
            List<int> data = new List<int>();
            int currentNum = 0;
           
            //create and enter loop
            while (true)
            {
                if (currentNum % 2 == 0)
                {
                    //add the data to the IEnumerable container
                    data.Add(currentNum);
                }
                if (currentNum >= maxCount)
                {
                    //limit has been reach so we exit loop
                    break;
                }
            }
            //return the IEnumerable dataset
            return data;
        }


New Methodology

The new yield keywords allow you to shorten your code to only the following steps
  1. Loop through some data. 
  2. yield return within the loop 
  3. yield break if a limit is reached before the loop is completed. 
There is no need to explicitly create or return your IEnumerable collection.

The yield return statement appends a value to the IEnumerable collection. The yield break statement exits any looping construct to prevent more items being added to the return set. If you omit yield break, the loop exits as it normally would.

Below is an example using the yield keyword for the process required.

public IEnumerable<int> GetEvenNumbers(int maxCount)
        {
            int currentNum = 0;

            //create and enter loop
            while (true)
            {
                if (currentNum % 2 == 1)
                {
                    //add the data to the IEnumerable container using yield return
                    yield return currentNum;
                }
                if (currentNum >= maxCount)
                {
                    //limit has been reach so we exit loop using yield break
                    yield break;
                }
            }
        }


Performance

The difference between using the yield return and simply returning a set is subtle. Returning a set at the end of the method returns the entire set at once. The client must wait until the method is complete before it can use any values in the return set. Each time the yield return statement executes, the client has access to another element in the set. Essentially the set trickles back a little at a time. This can provide a gain in performance or perceived performance if you have a large set to return.

 Limitations

There are a few restrictions to using the yield keywords

  1. These keywords cannot appear in blocks marked "unsafe" 
  2. These keywords cannot appear in a method with ref or out parameters 
  3. The yield return statement is not allowed inside a try-catch block, although it may be located inside a try-finally block. 
  4. A yield break statement may not be located inside a finally block. 


Other Blogs and Sites

Social Media