Friday, 7 February 2014

Structures

Structures or structs are user-defined data types that can contain other variables as its fields, and methods as its bahavior. You can create your very own customized data type using structures.Let say you want a data that don't just store a name as a string, but you also want to include his age and his monthly salary. To define a structure in a program, we use the following syntax.

struct StructName
{
   member1;
   member2;
   member3;
   ...
   member4;
}

We declare a structure using the struct keword. Names of structures should used Pascal Casing to indicate that it is a structure but that is just a convention followed in C# programming. The members can either be variables or methods. The program below shows an example of a structure.

using System;

namespace StructuresDemo
{
    public struct Employee    
    {                         
        public string name;   
        public int age;       
        public decimal salary;
    }                         

    public class Program
    {
        public static void Main()
        {
            Employee employee1;
            Employee employee2;

            employee1.name = "Jack";
            employee1.age = 21;     
            employee1.salary = 1000;

            employee2.name = "Mark";
            employee2.age = 23;     
            employee2.salary = 800; 

            Console.WriteLine("Employee 1 Details");
            Console.WriteLine("Name: {0}", employee1.name);
            Console.WriteLine("Age: {0}", employee1.age);
            Console.WriteLine("Salary: {0:C}", employee1.salary);

            Console.WriteLine(); //Seperator

            Console.WriteLine("Employee 2 Details");
            Console.WriteLine("Name: {0}", employee2.name);
            Console.WriteLine("Age: {0}", employee2.age);
            Console.WriteLine("Salary: {0:C}", employee2.salary);
        }
    }
}


Output
Employee 1 Details
Name: Jack
Age: 21
Salary: $1000.00

Employee 2 Datails
Name: Mike
Age: 23
Salary: $800.00

Lets disect the code to better understand the program.
the declaration of a structure
public struct Employee    
    {                         
        public string name;   
        public int age;       
        public decimal salary;
    }        
 Note that we used the public keyword. That keyword simply indicates that our Employee 
can be accessed or used anywhere in the program (or outside the program). public is one 
of the access specifiers. We used the struct keyword followed by name of the structure.
Structure names also follow the Pascal Casing convention. 
 Inside the body of the structure, we defined three fields. Fields are like the 
properties of our Employee. For example,an employee has a name, an age and  his job, 
he is given a monthly salary. These members was also declared as public so they can 
be called outside the structure as demonstrated later. 

declares two instances of  Employee
 Employee employee1;
 Employee employee2;
 
 declares two instances of  Employee. The declaration of structure instances is similar 
to normal variables. You first indicate the structure type, and then the 
identifier. 

 assigned values to the respective fields of each employee.
            employee1.name = "Jack";
            employee1.age = 21;     
            employee1.salary = 1000;
            employee2.name = "Mark";
            employee2.age = 23;     
            employee2.salary = 800; 
 assigned values to the respective fields of each employee. To access the fields outside the 
structure, they must be  public first .We first write the name of the variable, followed by a dot (.)
and then the name of the field.When the dot operators is used in this context, it is called the member access 
operator as it allows you to call the members of a particular structure.
  
 demonstrates how you can access the values stored on each field of a structure variable 
            Console.WriteLine("Employee 1 Details");
            Console.WriteLine("Name: {0}", employee1.name);
            Console.WriteLine("Age: {0}", employee1.age);
            Console.WriteLine("Salary: {0:C}", employee1.salary);
            Console.WriteLine(); //Seperator
            Console.WriteLine("Employee 2 Details");
            Console.WriteLine("Name: {0}", employee2.name);
            Console.WriteLine("Age: {0}", employee2.age);
            Console.WriteLine("Salary: {0:C}", employee2.salary);
         
 demonstrates how you can access the values stored on each field of a structure variable. When accessing or reading
 a field, we do the same thing. We indicate the variable name followed by a dot and then the name of the field to be read.
 
Structures are value types. That means that if we assign  employee2 with a value of employee1,employee2 will 
copy all the values of the attributes of employee1 instead of getting its reference. A class is similar to a 
structure but it is a reference type. 

You can also add methods inside a structure. The example below modifies the program in Example 1.
 
using System;

namespace StructuresDemo2
{
    public struct Employee
    {
        public string name;
        public int age;
        public decimal salary;

        public void SayThanks()                          
        {                                                
            Console.WriteLine("{0} thanked you!", name); 
        }                                                
    }

    public class Program
    {
        public static void Main()
        {
            Employee employee1;
            Employee employee2;

            employee1.name = "Jack";
            employee1.age = 21;
            employee1.salary = 1000;

            employee2.name = "Mark";
            employee2.age = 23;
            employee2.salary = 800;

            Console.WriteLine("Employee 1 Details");
            Console.WriteLine("Name: {0}", employee1.name);
            Console.WriteLine("Age: {0}", employee1.age);
            Console.WriteLine("Salary: {0:C}", employee1.salary);

            employee1.SayThanks();

            Console.WriteLine(); //Seperator

            Console.WriteLine("Employee 2 Details");
            Console.WriteLine("Name: {0}", employee2.name);
            Console.WriteLine("Age: {0}", employee2.age);
            Console.WriteLine("Salary: {0:C}", employee2.salary);

            employee2.SayThanks();
        }
    }
}
 
 
 Output
Employee 1 Details
Name: Jack
Age: 21
Salary: $1000.00
Jack thanked you!

Employee 2 Details
Name: Mike
Age: 23
Salary: $800.00
Mike thanked you!
 
 
 
 
 define a new method inside a structure
 
        public void SayThanks()                          
        {                                                
            Console.WriteLine("{0} thanked you!", name); 
        }     
 
This method prints a message and gets the value of the name field to show a unique 
message for each instance. To call the method, we used similar syntax except that instead 
of the field following the dot, we indicate the name of method to call followed by parentheses—and
a set of arguments if they are required by a function.  

What is IntelliSense

IntelliSense serves as an autocompletion tool as well as a quick reference for classes, methods, and many more.IntelliSense is activated the moment you type a letter in the Code Editor. Type the following code inside the Main method of the program.

System.Console.WriteLine("Welcome to Visual C# Tutorials!");
 
 

Intellisense 01

IntelliSense gives you a list of recommendations with the most relevant one on the first of the list. You can press tab to accept the selected recommendation. Typing a dot will bring you another list of recommendations. The list is based on all the components that are contained inside the preceding namespace.



Intellisense 02

If you stay the selection for a short amount of time, the description about the selected item will show up. This allows you to learn what each item does without going to the full documentation. As you type the code, the list is narrowed down. For example, typing the letter W make IntelliSense to only show items with W's in it.

Intellisense 03
Typing more letters will narrow the list even further.

Intellisense 04
When working with methods with multiple overloads(versions), you can use the arrow heads to see all the possible overloads of the method.

Intellisense 05
You will also see the different parameters required by the method and their individual descriptions. Methods and parameters will be discussed in a later lesson.
IntelliSense is such a great feature, and for every release of Visual Studio, IntelliSense becomes even more intelligent in suggesting codes for you. It is one of the time-saving tools Visual Studio offers.