Unit 2
Unit 2
What is C#:
C# is a modern, general-purpose, object-oriented programming language
developed by Microsoft and approved by European Computer Manufacturers
Association (ECMA) and International Standards Organization (ISO).
C# was developed by Anders Hejlsberg and his team during the development of
.Net Framework. C# is designed for Common Language Infrastructure (CLI),
which consists of the executable code and runtime environment that allows use
of various high-level languages on different computer platforms and
architectures.
C# code is compiled as managed code. Combines the best features of Visual
Basic, C++ and Java
“C#, pronounced "C-sharp," is an object-oriented programming language from
Microsoft that enables developers to build applications that run on the .NET
platform. C# has its roots in the C family of programming languages and shares
many of the same characteristics as those found in C and C++, as well as in Java
and JavaScript”.
C# History:
.Net Framework is a software development platform developed by Microsoft
for building and running Windows applications. The .Net framework consists of
developer tools, programming languages, and libraries to build desktop and web
applications. It is also used to build websites, web services, and games.
The .Net framework was meant to create applications, which would run on
the Windows Platform. The first version of the .Net framework was released in
the year 2002.
The Microsoft .Net framework can be used to create both – Form-based
and Web-based applications. Web services can also be developed using the .Net
framework.
The framework also supports various programming languages such as Visual
Basic, C# , C++, and F#. So developers can choose and select the language to
develop the required application. C# and visual basic are the main
languages used in .Net framework.
Version .NET Framework Visual Studio
C# 1.0 .NET Framework Visual Studio .NET
1.0/1.1 2002
C# 2.0 NET Framework 2.0 Visual Studio 2005
C# 3.0 NET Framework 3.0/3.5 Visual Studio 2008
C# 4.0 NET Framework 4.0 Visual Studio 2010
C# 5.0 NET Framework 4.5 Visual Studio 20012/13
C# 6.0 NET Framework 4.6 Visual Studio 2013/15
Feature of C#:
1. Simple
2. Modern Programming Language
3. Object Oriented
4. Type Safe
5. Interoperability
6. Scalable and Updateable
7. Component Oriented
8. Structured Programming Language
9. Rich Library
10. Fast Update
1) Simple:
C# is a simple language in the sense that it provides structured approach (to
break the problem into parts), rich set of library functions, data types etc.
2) Modern Programming Language:
C# programming is based upon the current trend and it is very powerful and
simple for building scalable, interoperable and robust applications,
3) Object Oriented:
C# is object oriented programming language. OOPs makes development and
maintenance easier where as in Procedure-oriented programming language it is
not easy to manage if code grows as project size grow.
4) Type Safe:
C# type safe code can only access the memory location that it has permission to
execute, Therefore it improves a security of the program
5) Interoperability:
Interoperability process enables the C# programs to do almost anything that a
native C++ application can do.
6) Scalable and Updateable:
C# is automatic scalable and updateable programming language. For updating
our application we delete the old files and update them with new ones.
7) Component Oriented:
C# is component oriented programming language. It is the predominant
software development methodology used to develop more robust and highly
scalable applications.
8) Structured Programming Language:
C# is a structured programming language in the sense that we can break the
program into parts using functions. So, itis easy to understand and modify.
9) Rich Library:
C# provides a lot of inbuilt functions that makes the development fast.
Object:-
When a class is defined, only the specification for the object is defined; no
memory or storage is allocated. To use the data and access functions defined
in the class, you need to create objects.
In other words, object is an entity that has state and behavior. Here, state
means data and behavior means functionality. “Object is a basic runtime
entity, it is created at runtime”. Object is an instance of a class. All the
members of the class can be accessed through object.
Accessing data members and member functions:
The data members and member functions of class can be accessed using the
dot(‘.’) operator with the object. For example if the name of object is obj and
you want to access the member function with the name printName() then you
will have to write obj.printName() .
Syntax:
Classname objectname = new object();
Note:- Here new keyword is used for allocation of memory to data member.
Public class addition
{
Public int add(int a, int b)
{
int c = a + b;
return c;
}
}
Now access a class property
{
addition a = new addition()
a.add(10,20)
}
What is Method:
Method: “A method is a block of code which only runs when it is called”. You
can pass data, known as parameters, into a method. Method are used to
perform certain actions, and they are also known as functions. Why use
methods? To reuse code: define the code once, and use it many times.
Create a Method:
A method is defined with the name of the method, followed by parentheses ().
C# provides some pre-defined methods, which you already are familiar with,
such as Main(), but you can also create your own methods to perform certain
actions
Syntax:
AccessModifier returnType Method Name()
{
Body
}
Call a Method
To call (execute) a method, write the method's name followed by two
parentheses () and a semicolon;
Example:
Class Program{
Int addnumber(int a, int b) //Method Declare//
{
Int sum = a+b;
Return sum;
}
Static void main(string[] args ) {
Program p1 = new program();
Int sum = p1.addnumber(100,100) //Method call//
Console.WriteLine(“sum=” +sum);
Console.ReadLine();
}
}
}
Event:- Events are user actions such as key press, clicks, mouse movements, etc.,
or some occurrence such as system generated notifications. Applications need to
respond to events when they occur. For example, interrupts. Events are used for
inter-process communication
Using Delegates with Events:
The events are declared and raised in a class and associated with the event
handlers using delegates within the same class or some other class. The class
containing the event is used to publish the event. This is called the publisher class.
Some other class that accepts this event is called the subscriber class. Events use
the publisher-subscriber model.
A publisher is an object that contains the definition of the event and the delegate.
The event-delegate association is also defined in this object. A publisher class
object invokes the event and it is notified to other objects.
A subscriber is an object that accepts the event and provides an event handler.
The delegate in the publisher class invokes the method (event handler) of the
subscriber class.
What is Array:
An array stores a fixed-size sequential collection of elements of the same type.
An array is used to store a collection of data, but it is often more useful to think
of an array as a collection of variables of the same type stored at contiguous
memory locations.
Instead of declaring individual variables, such as number0, number1, ..., and
number99, you declare one array variable such as numbers and use numbers[0],
numbers[1], and ..., numbers[99] to represent individual variables. A specific
element in an array is accessed by an index. All arrays consist of contiguous
memory locations. The lowest address corresponds to the first element and the
highest address to the last element.
Declaring Arrays
To declare an array in C#, you can use the following syntax −
datatype[] arrayName;
Initializing an Array
Declaring an array does not initialize the array in the memory. When the array
variable is initialized, you can assign values to the array. Array is a reference
type, so you need to use the new keyword to create an instance of the array. For
example
double[] balance = new double[10];
Assigning Values to an Array
❖ You can assign values to individual array elements, by using the index
number, like −
double[] balance = new double[10];
balance[0] = 4500.0;
❖ You can assign values to the array at the time of declaration, as shown −
double[] balance = { 2340.0, 4523.69, 3421.0};
You can also create and initialize an array, as shown −
int [] marks = new int[5] { 99, 98, 92, 97, 95};
❖ You may also omit the size of the array, as shown −
int [] marks = new int[] { 99, 98, 92, 97, 95};
❖ You can copy an array variable into another target array variable. In such case,
both the target and source point to the same memory location −
int [] marks = new int[] { 99, 98, 92, 97, 95};
int[] score = marks;
C# Inheritance:
Inheritance: - it is a process in which one object acquires all the properties and
behaviours of its parent object automatically. In such way, you can reuse, extend
or modify the attributes and behaviours which are defined in other class.
The class which inherits the members of another class is called derived class and
the class whose members are inherited is called base class. The derived class is
the specialized class for the base class.
One of the most important concepts in object-oriented programming is
inheritance. Inheritance allows us to define a class in terms of another class,
which makes it easier to create and maintain an application. This also provides
an opportunity to reuse the code functionality and speeds up implementation time.
When creating a class, instead of writing completely new data members and
member functions, the programmer can designate that the new class should
inherit the members of an existing class. This existing class is called the baseclass,
and the new class is referred to as the derived class.
Inheritance (Derived and Base Class)
In C#, it is possible to inherit fields and methods from one class to another. We
group the "inheritance concept" into two categories:
Derived Class (child) - the class that inherits from another class
Base Class (parent) - the class being inherited from
Syntax:
class derived-class:base-class
{
// methods and fields
}
To inherit from a class, use the : symbol.
Types of inheritance:
1. Single Inheritance
In single inheritance, a single derived class inherits from a single base class.
2. Multilevel Inheritance
In multilevel inheritance, a derived class inherits from a base and then the
same derived class acts as a base class for another class.
3. Hierarchical Inheritance
In hierarchical inheritance, multiple derived classes inherit from a single base
class.2. Multilevel Inheritance
4. Multiple Inheritance
In multiple inheritance, a single derived class inherits from multiple base
classes. C# doesn't support multiple inheritance. However, we can achieve
multiple inheritance through interfaces.
5. Hybrid Inheritance
Hybrid inheritance is a combination of two or more types of inheritance. The
combination of multilevel and hierarchical inheritance is an example of
Hybrid inheritance.
2) TextBox:
A text box is used for allowing a user to enter some text on the Web form
application. Let’s see how we can implement this with an example shown below.
We will add one textbox to the form in which the user can enter his name.
Text box controls allow entering text on a form at runtime. By default, it takes a
single line of text, however, you can make it accept multiple texts and even add
scroll bars to it.
3) Button:
C# Button class in .NET Framework class library represents a Windows Forms
Button control. A Button control is a child control placed on a Form and used to
process click event and can be clicked by a mouse click or by pressing ENTER
The Button control represents a standard Windows button. It is generally used to
generate a Click event by providing a handler for the Click event.
4) List Box:
Listbox in C# provides an interface for the user to display multiple list items. The
user could select single or multiple elements from the list by clicking elements.
These elements are generally displayed in multiple columns instead of a straight
vertical list.
It allows the programmer to add items at design time by using the properties
window or at the runtime.
5) CheckBox:
The CheckBox control is the part of windows form which is used to take input
from the user. Or in other words, CheckBox control allows us to select single or
multiple elements from the given list or it can provide us options like yes or no,
true or false, etc. It can be displayed as an image or text or both.
6) Radiobutton: