Tuesday 6 September 2016

Data Type in C#

 

Data Types in C#


C# provides various built-in data types. Built-in data types are predefined data types that can be directly used in a program to declare variables.
Built-in                  #Bytes                            Values
Data   Type  
       
Char                     2                                     0 to 65535
       
Int                        4                                    -2,147,483,648 to 2,147,483,647
       
Float                    4                                    -3.402823E+38 to -1.401298E-45 (for negative values)
                                                                  1.401298E-45 to 3.402823E+38 (for positive values)
       
Double                 8                                    -1.79769313486232E308 to -4.94065645841247E-
                                                                   324 (for negative values) and 4.94065645841247E-
                                                                   324 to 1.79769313486232E308 (for positive values)
Bool                    1                                    True or False
       
String               Variable                           0-2 billion Unicode characters
                         length                                  

The #Bytes column in the preceding table specifies the bytes that are required to store the variable in the memory. The Values column specifies the range of values that can be stored in the variable.

    Types of Data Types

C# supports the following data types:
  • Value types: The value types directly contain data. Some examples of value types are char, int, and float, which can be used for storing alphabets, integers, and floating point values, respectively. When you declare an int variable, the system allocates memory to store the value. The following figure shows the memory allocation of an int variable.
  • Reference types: The reference type variables, instead of containing data, contain a reference (address) to the data stored in the memory.More than one reference type variable can be created to refer to the same memory location. This means that if the value in the referenced memory location is modified, all the referring variables automatically reflect the changed value. The example of a reference type is the string data type. The following figure shows the memory allocation of a string value HELLO in a variable named Str.

Variable in C#

 

Variable

Consider a situation where you have to create a program that accepts two numbers from a user and displays the sum of the numbers on the screen. Now, while reading the numbers provided by the user, you need to store these numbers somewhere in the memory so that you can perform the add operation on the numbers. You can store the numbers in the memory by using variables.

We can say thar variables are the bukket which is use to store the information.

Naming Variables in C#

In C#, the following rules are used for naming variables:

  • A variable name must begin with a letter or an underscore („_‟), which may be followed by a sequence of letters, digits (0-9), or underscores. The first character in a variable name cannot be a digit.
  • A variable name should not contain any embedded spaces or symbols,such as ? ! @ # + - % ^ & * ( ) [ ] { } . , ; : " ' / and \.
  • However, an underscore can be used wherever a space is required, like High_Score.
  • A variable name must be unique. For example, to store four different numbers, four unique variable names need to be used.
  • A variable name can have any number of characters.
  • Keywords cannot be used as variable names. For example, you cannot declare a variable named class as it is a keyword in C#.

"C# is a case-sensitive language. This means that the TennisPlayerName variable is not the same as the tennisplayername variable. In other words, Uppercase letters are considered distinct from lowercase letters."

You can declare and initialize variables by using the following syntax:

<data_type> <variable_name>=<value>;

In the preceding syntax, the <data_type> represents the kind of data type that will be stored in a variable and <value> specifies the value that needs to be stored in the variable.

Consider the following statement that declares a variable:

int age = 1;

The preceding statement declares a variable named age of the int data type. In addition, the statement initializes the variable with the value, 1. The int data type is used to store numeric data (integers).

Consider the following statement:

char choice='y';

The preceding statement declares the variable choice of the char data type and initializes the variable with the value, y.

Thursday 1 September 2016

Three Layer of MVC

The Three Layers of a Web Application

All applications can be broken into three layers. Each layer has its own components and functionality. The following figure depicts the various layers that constitute a Web application.

As displayed in the preceding figure, an application has the following three layers:

  • Presentation layer: Consists of the interface through which the users interact with the application.
  • Business logic layer: Consists of the components of the application that control the flow of execution and communication between the presentation layer and the data layer.
  • Data layer: Consists of components that expose the application data stored in databases to the business logic layer.

Architecture of a Web Application


The architecture of a Web application is the manner in which layers are distributed and the way in which they communicate with each other. Most applications are built by using all the three layers.

An application can have one of the following types of architectures:
  • Single-tier architecture
  • Two-tier architecture
  • Three-tier architecture
  • N-tier architecture

Single-tier Architecture

In an application based on single-tier architecture, all the three layers are integrated together and can be installed on a single computer. If the application needs to be accessed on multiple computers, a separate installation is required on each computer. For example, Adobe Photoshop that is used to create and edit graphics is a standalone application based on the single-tier architecture. The following figure displays the single-tier architecture.


Two-tier Architecture


In an application based on two-tier architecture, the three layers are distributed over two tiers, a client and a server. The presentation layer resides on each client computer, the business logic layer resides either on the client or on the server, and the data access layer resides on the server.

Depending on the business requirements, an organization can have the following types of two-tier application architecture:



  •  Fat client and thin server: The architecture in which the business logic layer resides on the client is known as the fat client and thin server architecture. In this architecture, the client accepts user requests and processes these requests on its own. The client communicates with the server only when the data for communication or archival needs to be sent to the server.

  • Fat server and thin client: The architecture in which the business logic layer resides on the server is known as the fat server and thin client architecture. In this architecture, the client accepts requests from the users and forwards the same to the server. Further, the server processes these requests and provides responses.

    The two-tier architecture can be used when multiple users need to access and manipulate common data storage. For example, an application needs to be developed to store an organization’s employee details in a database. In addition, the application should allow the retrieval of employee details from the database. In this case, the employee details need to be entered and retrieved by using multiple computers. Therefore, installing a database, along with the application on each computer, may lead to storage of duplicate records. In addition, a user cannot retrieve the employee details stored on some other computer. To avoid these problems, the application can be installed on each computer. However, the database is installed on a single computer, and the same can be accessed by the applications on multiple computers.

    In the preceding example, the presentation and business logic layers are integrated and installed on each computer. The applications installed on the computers send requests to the server (database) for the storage and retrieval of data. On the other hand, the server accepts the requests and responds accordingly. In this setup, the fat client and thin server architecture is used. The following figure displays the fat client and thin server architecture.


    In the preceding setup, the server only processes the data processing requests sent by the clients. Therefore, data retrieval is fast. As the business logic resides on the client, the other applications installed on the clients respond slowly. Further, if the business logic needs to be updated, the updates need to be installed on each client, which is a time-consuming process.

    To overcome these limitations, the business logic layer can be placed on the server along with the data layer. In this way, only the presentation layer will be available on the clients. This, in turn, results in a faster response from the other applications installed on the clients. In addition, if the business logic needs to be updated, the updates need to be installed at only one location, which is on the server, rather than on each client. Such a setup is an example of the thin client and fat server architecture.

    The following figure displays the thin client and fat server architecture.

 Three-tier Architecture

In an application based on three-tier architecture, the three layers of the application are placed separately as three different entities. This architecture is used for those applications in which merging the business logic layer with the presentation layer or the data layer may degrade the performance of the application. To improve the application performance, the three layers are kept separately and the communication among the layers occurs with the help of a request-response mechanism. The following figure displays the three-tier architecture

In the preceding figure, the business logic layer resides neither on a client nor on a database server. Instead, it resides on a separate server, which is known as an application server.

N-tier Architecture

The three-tier architecture is implemented when an effectively-distributed client/server architecture, which provides increased performance and scalability, is needed. However, it becomes a tedious task to design and deploy applications based on the three-tier architecture. The separation of presentation, business logic, and database access layers is not always obvious, because some business logic needs to appear on all the layers. Therefore, there is a need to further divide these layers. This requirement has led to the expansion of the three-tier application architecture to the N-tier application architecture.

A common approach to implement the N-tier architecture involves further separation of the presentation layer and the data layer. For example, the
presentation layer may be divided into the GUI layer and the presentation logic layer. Similarly, the data layer may be divided into the data access layer and the data layer.

The N-tier architecture separates the various business processes into discrete units of functionality called services. Each service implements a set of related business rules that defines what needs to be done and how it needs to be done within an organization.

The N-tier application architecture provides improved flexibility and scalability of applications, as compared to the three-tier application architecture. This is because the functioning of each layer is completely hidden from other layers, which makes it possible to change or update one layer without recompiling or modifying the other layers.


Classes in C#

 

Classes in C#

C# classes are the primary building blocks of the language. C# also provides certain predefined set of classes and methods. These classes and methods provide various basic functionalities that you may want to implement in your application.

For example, if you want to perform some input/output operations in your application, you can use predefined classes available in the System.IO namespace.

NOTES:-

"A namespace is a collection of classes. C# provides some predefined namespaces that contain classes, which provide commonly used functionality. To use the classes defined in these namespaces, you need to include the relevant namespace in your program.
You can also create your own namespaces that contain classes that you need to use across several programs."


In addition to using the predefined classes, you can also define your own classes. Consider the following code, which defines a class named Hello:

public class Hello

{
 public static void Main(string[] args)

{

System.Console.WriteLine("Hello, World! \n");

}

}

The preceding class declaration includes the method, Main() that will display the message, “Hello, World!” on your screen. The preceding code includes the following components:

  • The class keyword
  •  The class name
  •  The Main() method
  •  The System.Console.WriteLine() method
  •  Escape sequences

Let us discuss these components in detail.


The class Keyword


The class keyword is used to declare a class. In the preceding code, the class keyword declares the class, Hello.


The Class Name


The class keyword is followed by the name of the class. In the preceding code, Hello is the name of the class defined by using the class keyword.


Class Naming Conventions in C#


Class names should follow certain naming conventions or guidelines. A class name:

  •  Should be meaningful (strongly recommended).
  •  Should ideally be a noun.
  •  Can use either the Pascal case or Camel case. In Pascal case, the first letter is capitalized and the rest of the letters are in lower case, such as

Myclass. In Camel case, the first letter is in lower case and the first letter of each subsequent word is capitalized, such as           myClass                 or           intEmployeeDetails.

Rules for Naming Classes in C#

In addition to the conventions, there are certain rules that must be followed while naming classes in C#. The name of classes:

  •   Must begin with a letter. This letter may be followed by a sequence of letters, digits (0-9), or „_‟. The first character in a class name cannot be a digit.

  •   Must not contain an embedded space or a symbol like ? - + ! @ # % ^ &     *    ( ) [ ] { } . , ; : " ' */ and \. However, an underscore („_‟) can be used wherever a space    is   required.
  •  Must not use a keyword for a class name. For example, you cannot declare a class called public.

The Main() Method


  • The first line of code that a C# compiler looks for in the source file is the Main() method. This method is the entry point for an application. This means that the execution of code starts from the Main() method.
  •  The Main() method is ideally used to create objects and invoke the methods of various classes that constitute the program.
NOTES"-

"string[] args in the preceding code is an optional argument passed to the Main() method. Arguments that are passed to methods will be explained later in the
course"


"You must include a Main() method in a class to make your program executable."


The System.Console.WriteLine() Method


Console is a class that belongs to the System namespace. The Console class includes a predefined WriteLine() method. This method displays the enclosed text on the user‟s screen. The Console class has various other methods that are used for various input/output operations.

The dot character (.) is used to access the WriteLine() method, which is present in the Console class of the System namespace.

"Referencing a method in this form is also known as referencing a method by using a fully qualified name."

The System.Console.WriteLine() statement can also be written as Console.WriteLine() if the statement, using System is included as the first line of the code.

The following code snippet is an example of the Console.WriteLine() method:

Console.WriteLine("Hello, World! \n");

The preceding code snippet will display the following message on the screen:

Hello, World!
"The WriteLine() method is used to write on the screen. To read the input provided by the users, you can use the ReadLine() method. The method will be discussed later during the course."

Wednesday 31 August 2016

ASP(MVC)

Introduction to Web Application Development using


ASP.NET MVC


Web applications have revolutionized the way business is conducted. These applications enable organizations to share and access information from anywhere, anytime. This has majorly moved the focus of application development from desktop applications to Web applications. Today, one of the most popular server-side technologies used for developing Web applications is ASP.NET.

This chapter introduces the basics of Web application development. It also discusses the layers, architecture, and different ways of scripting a Web application. In addition, it discusses the different types of Web development platforms and explains the ASP.NET MVC platform in detail.

Objectives


In this chapter, you will learn to:

  • Identify the basics of Web application development
  • Explore ASP.NET

Introduction to Web Application Development


Among all technologies, the Internet has been the fastest growing technology. Ever since its inception, the Internet has evolved exponentially. In the recent years, it has changed the way business is conducted.

Prior to the evolution of the Internet, organizations could deliver only limited information to their prospective clients by using the existing communication media. However, with the inception of the Internet, organizations found a new medium to reach a larger range of people, irrespective of their geographical locations. Therefore, organizations increasingly became dependent on the Internet for sharing and accessing information. This resulted in changing the focus of application development from desktop applications to Web applications.


Defining Web Applications



Web applications are programs that are executed on a Web server and accessed from a Web browser. These applications enable organizations to share and access information on the Internet and corporate intranets. This information can be accessed from anywhere, anytime. In addition, Web applications can support online commercial transactions, popularly known as e-commerce. An online store accessed through a Web browser is an example of a Web application.

The following figure shows the working of a Web application.



 In the preceding figure, a client sends a request for a resource, such as a Web page or a video, on the Internet. The Web server interprets the client request and determines the type of resource requested by the client. If the required resource is found, the Web server sends the resource to the client. Otherwise, an error message is sent to the client.










Sunday 28 August 2016

C#


Introduction to C#


A computer needs a set of instructions called a program to perform any operation. A program needs to be written in a specific language called programming language, so that the computer can understand the instructions. C# is one such programming language.

This chapter introduces you to C#. It explains how to define classes and declare variables in C#. In addition, it discusses how to create the object of a class. Further, it discusses how to write and execute C# programs.

Objectives


In this chapter, you will learn to:

  •    Identify C# basics
  •    Declare variables
  •    Write and execute C# programs

Introducing C#


Computer languages have come a long way since the 1940s. During that period, scientists punched instructions into large, room-sized computer systems. These instructions were given in machine language, which consisted of a long series of zeroes and ones. These machine language instructions were executed directly by the CPU. The machine language is called the First Generation of computer languages.

The 1950s saw the emergence of the Second Generation of computer languages, the assembly language. Assembly language is easier to write than machine language but still extremely complicated for a common man. However, the computer could still understand only machine language. Therefore, the Assembler software was developed to translate the code written in assembly language into machine language.

In 1967, Martin Richard developed a language called BPCL for writing operating systems. An operating system is a set of programs that manages the resources of a computer and its interactions with users. The era of the Third Generation of computer languages had arrived. In 1970, Ken Thompson modified BPCL to create a new language called B. While working for Bell Laboratories, Thompson teamed up with Dennis Ritchie and wrote an initial version of the Unix operating system for a DEC PDP-7 computer.