Thursday, October 23, 2014

.NET platform of languages





  • support many programming languages C#, VB.NET, F# and VC++
  • Advantages of .NET         
                     The programs created using .NET are very efficient in performance.
                     The new technologies introduced into .NET like WPF, Silverlight are very useful to                              create rich  User Interfaces
                                    Microsoft Silverlight is an application framework for writing and running rich Internet applications, with features and purposes similar to those of Adobe Flash. The run-time environment for Silverlight is available as a plug-in for web browsers running under Microsoft Windows and Mac OS X.

                     Can code smart as well as fast.



  • namespace declaration
  • a class
  • class methods
  • class attributes
  • a main method
Hello World Program

#provide a shorthand way to access various parts of the .NET Framework that you might want to use in your program.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program  #real name of the class is ConsoleApplication1.Program
    {
        static void Main(string[] args) #The static keyword indicates that you can access this method without                                                                     having an object of your class available.
(Objects are instances of class. And normally methods are called only if u have an object. Static methods are exceptions)
        {
            Console.WriteLine("Hello, World");
            Console.ReadLine();
        }
    }
}



It's worth to note the following points:
  • C# is case sensitive.
  • All statements and expression must end with a semicolon (;).
  • The program execution starts at the Main method.
  • Unlike Java, file name could be different from the class name

You can compile a C# program by using the command-line instead of the Visual Studio IDE:
  • Open a text editor and add the above-mentioned code.
  • Save the file as helloworld.cs
  • Open the command prompt tool and go to the directory where you saved the file.
  • Type csc helloworld.cs and press enter to compile your code.
  • If there are no errors in your code, the command prompt will take you to the next line and would generate helloworld.exe executable file.
  • Next, type helloworld to execute your program.
  • You will be able to see "Hello World" printed on the screen.

.NET  Framework Platform Architecture


C# features  =>  generics, interfaces, delegates, lambda expressions, and LINQ.

You can use C# language to develope four types of Applications

  • Console applications   =>  runs in a console window
  • Windows applications =>   runs on a PC's desktop eg : Microsoft word, Excel and other widgets on a                                                      desktop
  • ASP.NET applications  =>   runs on a web server and delivers its functionality through a browser such as I                                               nternet Explorer or Firefox, typically over the Web. ASP.NET technology                                                           facilitates developing web applications quickly and easily
  • Web services               =>    complex applications that can be accessed using standard Internet protocols,                                                 and that can provide services such as current stock quotes,


Getting input => Console.ReadLine();
Giving output => Console.WriteLine();
Converting to  => Convert.To()

  1. Get an input and convert to another type
                        string whatTheUserTyped = Console.ReadLine();
             int aNumber = Convert.ToInt32(whatTheUserTyped);

     2. Arrays  =>  An array is a way of keeping track of a list or collection of many related things all together
                             You can have arrays of basically anything. ints, floats, bools.
                             int[] scores = new int[10];
  • Arrays store collections of related objects of the same type.
  • To create an array, you use the square brackets and the new keyword: int[] scores = new int[10];.
  • You can also create arrays by giving specific values: int[] scores = new int[] { 1, 2, 3, 4, 5 };
  • You can access and modify values in an array with square brackets as well: int firstScore = scores[0]; and scores[0] = 44;
  • Indexing of arrays is 0-based, so 0 refers to the first element in the array.
  • You can create arrays of arrays: // int[][] grid;
  • You can also create multi-dimensional arrays: //int[,] grid = new int[5, 4];
  • You can use the foreach loop to easily loop through an array and do something with each item in the array: //foreach(int score in scores) { /* … */ }

       Accessing elements in the array =>    int fourthScore = scores[3];
                                       int eighthScore = scores[7];

       Other ways of initializing an array =>
                          int[] scores = new int[10] { 100, 95, 92, 87, 55, 50, 48, 40, 35, 10 };
               int[] scores = new int[] { 100, 95, 92, 87, 55, 50, 48, 40, 35, 10 };
     
    Length of  an array   =>
              int totalThingsInArray = scores.Length; // See, I told you it would be easy...
              Console.WriteLine("There are " + totalThingsInArray + " things in the array.")


  •       Calculate the minimum value of an array    
       int[] array = new int[] { 4, 51, -7, 13, -99, 15, -8, 45, 90 };
 
int currentMinimum = Int32.MaxValue; // We start really high, so that any element in the array will be lower that this.
 
for(int index = 0; index < array.Length; index++)
{
    if( array[index] < currentMinimum )
    {
        currentMinimum = array[index];
    }
}

       Arrays of Arrays  => 

jagged array ->  each array within the main array has diff length
int[][] matrix = new int[4][];
matrix[0] = new int[4];
matrix[1] = new int[5];
matrix[2] = new int[2];
matrix[3] = new int[6];
 
matrix[2][1] = 7

squre or rectangular  array ->  each array within the main array has same length

There's another way to work with arrays of arrays, assuming you want a rectangular array (which is almost always the case). This is called a "multi-dimensional array"
  
int[,] matrix = new int[4, 4];
matrix[0, 0] = 1;
matrix[0, 1] = 0;
matrix[3, 3] = 1;

To work with Arrays => Foreach

int[] scores = new int[10];
 
// Populate data and maintain it as your program runs. The below is just an example.
scores[0] = 42;
scores[5] = -1;
 
foreach (int score in scores)
{
    Console.WriteLine("Someone had this score:  " + score);
}


Enumerations

  • a way of defining your own type of variable so that it has specific values
  • public enum DaysOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
  • Enumerations should be defined outside of any classes that you have.
  • You can assign your own numeric values to the items in an enumeration: public enum DaysOfWeek { Sunday = 5, Monday = 6, Tuesday = 7, Wednesday = 8, Thursday = 9, Friday = 10, Saturday = 11 }
  • you make an enumeration outside of any classes that you have

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Net;
 
namespace EnumerationTutorial
{
    // You define enumerations outside of other classes.
    public enum DaysOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
 
    public class Program
    {
        static void Main(string[] args)
        {
 
        }
    }
}
create a variable of that type and assign a value with DOT(.) operator

DaysOfWeek today = DaysOfWeek.Sunday;
 
if(today == DaysOfWeek.Sunday)
{
    // ...  
}

More Details of Enumerations
C# is simply wrapping our enumeration around ints. 
When you create an enumeration, it starts off giving them values, starting with 0, 
We can do casting too
              int dayAsInt = (int)DaysOfWeek.Sunday;
        DaysOfWeek today = (DaysOfWeek)dayAsInt; // but this is an implicit cast, so...
        DaysOfWeek tomorrow = (dayAsInt + 1);