AWP-Notes

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

ADVANCED WEB PROGRAMMING

Introducing .NET
The .NET Framework
 The .NET Framework is a software development framework developed by Microsoft that provides a
runtime environment and a set of libraries and tools for building and running applications on Windows
operating systems.
 Includes a variety of programming languages, such as C#, F#, and Visual Basic, and supports a range of
application types, including desktop, web, mobile, and gaming applications.
 The .NET Framework includes two main components: the Common Language Runtime (CLR) and the
.NET Framework Class Library.
 The CLR is responsible for managing the execution of code written in any of the supported
languages, while the class library provides a large set of pre-built functions and classes that can be
used to create a wide range of applications.
 Key advantages of the .NET Framework
- Run variety of programming languages
- Support for a variety of application types.
- Features that help improve the security, reliability, and performance of applications.
 Designed to integrate with other Microsoft technologies, such as Microsoft SQL Server, Microsoft
SharePoint, and Microsoft Office
 It is used to develop Form-based applications, Web-based applications, and Web services.

C#
 C# is the most common programming language used to develop multiple applications in the.NET
framework, and it was introduced by Microsoft in 2000.
 It was designed to be a simple, object-oriented programming language that can be used to create a wide
range of applications and software.
 It is platform-independent in the sense that it may be used to create programmes that operate on different
platforms such as Windows, macOS, Linux, and mobile devices. This makes C# a versatile language.
 Prerequisite to Learn C# : C and C++

VB
 Visual Basic is an object-oriented programming language developed by Microsoft in 2002
 It is a simple, high-level, object-oriented programming language. It supports the OOPs concept, such as
abstraction, encapsulation, inheritance, and polymorphism. Therefore, everything in the VB.NET
language is an object
 It is not a case sensitive language
 Using Visual Basic makes it fast and easy to create type-safe .NET apps.

.NET Languages
 C# - Microsoft's flagship .NET Framework language which bears similarities to the C++ and Java
languages.
 Visual Basic .NET - A completely redesigned version of the Visual Basic language for the .NET
Framework.
 VBx - a dynamic version of Visual Basic .NET that runs on top of the Dynamic Language Runtime.
 C++/CLI and the deprecated Managed C++ - A managed version of the C++ language.
 J# - A Java and J++ .NET transitional language.
 JScript .NET - A compiled version of the JScript language.
 Windows PowerShell - An interactive command line shell/scripting language which provides full access
to the .NET Framework.
 IronPython - A .NET implementation of the Python programming language developed by Jim Hugunin
at Microsoft.
 IronRuby - A dynamically compiled version of the Ruby programming language targeting the .NET
Framework.
 F# - a member of the ML programming language family.
The Common Language Runtime
 The Common Language Runtime (CLR) is a component of the Microsoft .NET Framework that manages
the execution of .NET applications.
 It is responsible for loading and executing the code written in various .NET programming languages,
including C#, VB.NET, F#, and others
 When a C# program is compiled, the resulting executable code is in an intermediate language called
Common Intermediate Language (CIL) or Microsoft Intermediate Language (MSIL). This code is
not machine-specific, and it can run on any platform that has the CLR installed.
 When the CIL code is executed, the CLR compiles it into machine code that can be executed by the
processor.
 Services:
- Memory Management
- Safety, security, and exception handling.
- It also provides Just-In-Time (JIT) compilation, which compiles the CIL code into machine code on
the fly as the program runs, optimizing performance.
- Also provides .NET Framework Class Library
- It is thus, a run-time environment in the .NET Framework that runs the codes and helps in making
the development process easier by providing the various services.
 Main components of CLR:
 Common Language Specification (CLS):
- It is responsible for converting the different .NET programming language syntactical rules and
regulations into CLR understandable format. Basically, it provides Language Interoperability.
 Common Type System (CTS):
- Every programming language has its own data type system, so CTS is responsible for understanding
all the data type systems of .NET programming languages and converting them into CLR
understandable format which will be a common format.
- There are 2 Types of CTS that every .NET programming language have :
- Value Types: Value Types will store the value directly into the memory location. These types work
with stack mechanisms only. CLR allows memory for these at Compile Time.
- Reference Types: Reference Types will contain a memory address of value because the reference
types won’t store the variable value directly in memory. These types work with Heap mechanism.
CLR allot memory for these at Runtime.
 Garbage Collector:
- It is used to provide the Automatic Memory Management feature
 JIT(Just In Time Compiler):
- It is responsible for converting the CIL(Common Intermediate Language) into machine code

The .NET Class Library.


 .NET Framework Class Library is the collection of classes, namespaces, interfaces and value types that
are used for .NET applications.
 Following functions are supported:
- Base and user-defined data types
- Support for exceptions handling
- Input/output and stream operations
- Communications with the underlying system
- Access to data
- Ability to create Windows-based GUI applications
- Ability to create Web-Client and Server applications
- Support for creating web services
The C# Language

C# Language Basics
 C# (C-Sharp) is a programming language developed by Microsoft that runs on the .NET Framework.
 C# is used to develop web apps, desktop apps, mobile apps, games and much more.
 It is an object-oriented programming language.
 C# has roots from the C family, and the language is close to other popular languages like C++ and Java.
 The first version was released in year 2002. The latest version, C# 12, was released in November 2023.
 C# is used for:
 Why Use C#?
- It is one of the most popular programming languages in the world
- It is easy to learn and simple to use
- It has huge community support
- C# is an object-oriented language which gives a clear structure to programs and allows code to be reused,
lowering development costs
- As C# is close to C, C++ and Java, it makes it easy for programmers to switch to C# or vice versa
- Case Sensitive

Sample Code:
using System; //namespace: different classes under a single name , helps organize code
namespace HelloWorld
{
class Program {
static void Main(string[] args)
{
Console.WriteLine("Hello World!"); // Console: System namespace, consisting of WriteLine, Write
}
}
}
Variables and Data Types

 Variables are containers for storing data values.


 A data type specifies the size and type of variable values.
 It is important to use the correct data type for the corresponding variable; to avoid errors, to save time and
memory, but it will also make your code more maintainable and readable.
- int - stores integers (whole numbers), without decimals, such as 123 or -123
- double - stores floating point numbers, with decimals, such as 19.99 or -19.99
- char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
- string - stores text, such as "Hello World". String values are surrounded by double quotes
- bool - stores values with two states: true or false
 Declaring (Creating) Variables: type variableName = value; (string name=’Sophia’;)

C# Constants
 Make use of const keyword, which will declare a variable as constant (unchangeable)
const int myNum = 15;
myNum = 20; // error

C# Identifiers
 Variables must be identified with uniques names
 The general rules for naming variables are:
- Names can contain letters, digits and the underscore character (_)
- Names must begin with a letter or underscore
- Names should start with a lowercase letter, and cannot contain whitespace
- Names are case-sensitive
- Reserved words (like C# keywords, such as int or double) cannot be used as names
Variable Operations
 You can perform various operations on numbers ( E, M, D, A, S)
 Also () parentheses can be used
int number;
number = 4 + 2 * 3; // number will be 10.
number = (4 + 2) * 3; // number will be 18.

 Division can sometimes cause confusion in C. For example, if you divide 5 by 2, (Ans: 2.5, but we get 2)
 Addition operator ( +) can be used to join two strings
myName = firstName + " " + lastName; // Join three strings together.
 Shorthand Assignment Operators.
myValue += 10;
myValue *= 3;
myValue /= 12;
Advanced Math
 To use the math operations, you invoke the methods of the System.Math class.
 These methods are static, which means they are always available and ready to use.
double myValue;
myValue = Math.Sqrt(81); =9.0
myValue = Math.Round(42.889, 2); = 42.89
Math.Abs(-10); = 10.0, Math.Log(24.212); = 3.18.. (and so on), Math.PI; = 3.14.. (and so on)

Type Conversions
 Converting information from one data type to another is a fairly common programming task.
 You might need to take a calculated value and transform it into text you can display in a web page.
 Conversions are of two types: widening and narrowing.
 Widening conversions always succeed. For example, you can always convert a 32-bit integer into a 64-
bit integer. You won’t need any special code:
int mySmallValue;
long myLargeValue;
myLargeValue = mySmallValue;

 On the other hand, narrowing conversions may or may not succeed, depending on the data. If you’re
 converting a 32-bit integer to a 16-bit integer, you could encounter an error if the 32-bit number is larger
than the maximum value that can be stored in the 16-bit data type.
 To convert a variable, you simply need to specify the type in parentheses before the expression you’re
converting.
 The following code shows how to change a 32-bit integer to a 16-bit integer:
int count32 = 1000;
short count16;
count16 = (short)count32;
 This process is called casting. If you don’t use an explicit cast when you attempt to perform a narrowing
conversion, you’ll receive an error when you try to compile your code. However, even if you perform an
explicit conversion, you could still end up with a problem
Object-Based Manipulation
 Common data types have the built-in smarts to handle basic operations (such as counting the number of
characters in a string).
 For example, every type in the .NET class library includes a ToString() method. The default
implementation of this method returns the class name. In simple variables, a more useful result is returned:
the string representation of the given variable. The following code snippet demonstrates how to use the
ToString() method with an integer:
string myString;
int myInteger = 100;
myString = myInteger.ToString();

 The String Type


string myString = "This is a test string ";
myString = myString.Trim(); // = "This is a test string"
myString = myString.Substring(0, 4); // = "This"
myString = myString.ToUpper(); // = "THIS"
myString = myString.Replace("IS", "AT"); // = "THAT"
int length = myString.Length; // = 4

 The DateTime and TimeSpan Types

Three useful tasks:


• Extract a part of a DateTime (for example, just the year) or convert a TimeSpan to a specific
representation (such as the total number of days or total number of minutes)
• Easily perform date calculations
• Determine the current date and time and other information
DateTime myDate = DateTime.Now;
myDate = myDate.AddDays(100);
string dateString = myDate.Year.ToString();

The next example shows how you can use a TimeSpan object to find the total number of minutes between
two DateTime objects:
DateTime myDate1 = DateTime.Now;
DateTime myDate2 = DateTime.Now.AddHours(3000);
TimeSpan difference;
difference = myDate2.Subtract(myDate1);
double numberOfMinutes;
numberOfMinutes = difference.TotalMinutes;

The DateTime and TimeSpan classes also support the + and – arithmetic operators, which do the same
work as the built-in methods.

 The Array Type


int[] myArray = {1, 2, 3, 4, 5};
int numberOfElements;
numberOfElements = myArray.Length; // numberOfElements = 5

int[] myArray = {1, 2, 3, 4, 5};


int bound; // Zero represents the first dimension of an array.
bound = myArray.GetUpperBound(0); // bound = 4

int[,] intArray = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
int rows = intArray.GetUpperBound(0) + 1; // rows = 4
int columns = intArray.GetUpperBound(1) + 1; // columns = 2
Conditional Logic
Loops
Methods

You might also like