UNIT 1 -
PROGRAMMING
LECTURE 3 – INTRODUCTION TO C# AND .NET
TOPICS
Introduction to C# and .NET Basic usage of Visual Studio .NET
o Features of C# o Basic features
o .NET Framework o Create a new project
o C# basic syntax o Compile & Run
o Basic of debugging
Unit 1 - Programming / Lecture 2 - Basics of programming 2
INTRODUCTION
Introduced in 2000 by Microsoft
o Has roots in the C, C++, and Java
It’s appropriate for the most demanding app-development tasks
o Large-scale enterprise
o Web-based, mobile and “cloud”-based apps
OBJECT-ORIENTED PROGRAMMING
C# is object oriented
C# has access to the powerful .NET Framework Class Library
o Vast collection of built-in classes to develop app quickly
o We will learn more about .NET Framework later
EVENT-DRIVEN PROGRAMMING
C# is event driven.
o We write programs to respond to user-initiated events e.g., mouse clicks,
keystrokes, timer expiration, etc
o Or touches, finger swipes, etc on smartphones
C#
VISUAL PROGRAMMING
C# is visual programming language
o You can write code
o You can also use VS to drag/drop and design GUI
o Then VS will write the GUI code for you
o Allows us to focus on coding business processing
AN INTERNATIONAL STANDARD
C# has been standardized internationally
o Enables other implementation of language besides MS’s Visual C#
o One example is Mono that runs on Linux, iOS, Android, and Windows
INTERNET AND WEB PROGRAMMING
We can build web based app with C# and Microsoft’s ASP.NET
technology
MICROSOFT’S .NET
INTRODUCTION
In 2000 by Microsoft announce .NET initiative
o Broad vision for using the Internet and the web in the development,
engineering, distribution and use of software
o .NET permits you to create apps using any .NET-compatible langue (C#, Visual
Basic, Visual C++…)
o Part of the initiative includes ASP.NET technology
.NET FRAMEWORK
It executes apps and contains the Class Library
.NET Framework Class Library
o Contains many valuable prebuilt classes
o These classes are tested and tuned
o These speedup the development & performance
COMMON LANGUAGE RUNTIME (CLR)
CLR is another key part of the .NET Framework
o Executes .NET programs and provides functionality to make easier to develop and debug
CLR is a virtual machine (VM)
o It manages execution of programs and hides from them the underlying operating system hardware
o Source code are executed/managed by CLR is called managed code.
CLR provides many services to managed code
o Integrating software components written in different .NET languages
o Error handling between such components
o Enhanced security
o Automatic memory management
o Etc.
MANAGED CODE TO MACHINE INSTRUCTION
Managed code is compiled into machine-specific instructions in
following steps
1. First code is compiled into MSIL (all C#, J#, etc will be compiled into MSIL)
2. When the app executes, JIT compiler in the CLR translates MSIL into machine
code
3. The machine code executes on that platform
C#, C# or VB.NET compiler JIT in CLR
MSIL Machine
VB.NET,e
Source Language
tc
PLATFORM INDEPENDENCE
.NET Framework exists and is installed for a platform
.NET is platform independence
o Ability to run across multiple platforms
o E.g., we can install .NET in Mac OS
LANGUAGE INTEROPERABILITY
.NET Framework provides high level language interoperability
o Software can be written in C#, Visual Basic, etc
o All will be compiled into MSIL
C# BASIC SYNTAX
CREATING A NEW APP
THE SOURCE CODE
COMMENTS
Comments
o Are ignored by the compilers
o Used to clarify about the codes
Types of comments
o //: single-line comment
o /* ….*/: delimited comments
USING DIRECTIVE
A using directive tells compiler to look for a class that’s used in this app
C# provides thousand of classes to reuse
o These classes are organized into namespaces
o E.g., using System;
CLASS DECLARATION
Every app consists of at least one class declaration that defined by the programmer
o user-defined class
o E.g., public class Welcome1
Class Name convention (upper camel casing)
o Begin with a capital letter
o Capitalize the first letter of each word included
o Contains letters, digits, and underscore
o Doesn’t start with digit, doesn’t contain spaces
C# is case sensitive
o So be careful because Myname is different from MyName
C# METHOD AND STATEMENTS
Main method
o public static void Main(string[] args)
o It’s the starting point of every app
Statements
o Statements end with a semicolon (;)
MULTIPLE LINE STATEMENT
FORMATTING TEXT
DECLARE A VARIABLE
A variable is a name of a memory location
o Used to store value for the program
Variable must be declared before being used, a variable declaration
statement specifies
o The data type of the variable
o The name of the variable
o E.g., int number1;
DATA TYPES
Data type specifies kind of data a variable can store
C# Primitive types
o int: whole numbers e.g., 10, 11, -31914
o float, double: for real numbers e.g., 1.2 (store approximations of real numbers in
memory)
o decimal: stores real number precisely (28-29 digits), so can use with monetary
calculations
o char: for characters ‘a’, ‘b’, ‘c’
o There are many more types: bool, byte, short, long, etc.
READ THE INPUT
For standard input we use Console.ReadLine()
o String s = Console.ReadLine();
o int n = Convert.ToInt32(Console.ReadLine());
Possible erroneous input
o User can input a string which is not an integer
o In this case an Exception is raised
o We will learn about handling exception later
ASSIGNMENT OPERATORS (=)
Used to assign a value to a variable
o LHS = RHS;
o LHS must be a variable
o RHS is an expression (to be evaluated as a value)
ARITHMETIC OPERATORS
ACTIVITY: ADDING TWO NUMBERS
Please create a new program to
o Ask user to input two numbers
o Add two numbers
o Display the result