Lab 1
Lab 1
Lab # 1
Object:
Introduction to Object Oriented Programming with C#, .Net Framework and Visual Studio 2022.
Theory:
Introduction.
The fundamental idea behind object-oriented languages is to combine into a single unit both data and the
functions that operate on that data. Such a unit is called an object. An object’s functions, called member
functions in C#, typically provide the only way to access its data.
To read a data item in an object, member function in the object is called, which in turn will access the data
and return the value. The data cannot be accessed directly. The data is hidden, so it is safe from accidental
alteration. Data encapsulation and data hiding are key terms in object-oriented languages. This simplifies
writing, debugging, and maintaining the program.
A C# program typically consists of a number of objects, which communicate with each other by calling one
another’s member functions. Member functions are also called methods in some other object-oriented (OO)
languages. Also, data items are referred to as attributes or instance variables, features.
Introduction to C#
C# is a cross-platform language that can be used to create high-performance applications. C# was developed
by Anders Hejlsberg, as an extension to the C, C++ and Java language. C# gives programmers a high level
of control over system resources and memory.
Introduction of Headers
When you use a library function in a program, you must include the library function's header. The using
declaration is used to do this.
Header files in C# include a collection of predefined standard library functions. One requests that a header
file be used in his/her program by adding it with the preprocessing directive “using". The contents of a
header file may be used in our application by including it. A header file contains:
• Function definitions
• Data type definitions
It provides the functionalities by importing them into the application using the preprocessor instruction
"using". The preprocessor directives tell the compiler that these files must be handled before compilation.
4
Object Oriented Programming
Namespaces
A namespace in C# is a way to organize a set of related objects, such as classes, interfaces, and enums. A
namespace can help to avoid name conflicts and to create globally unique types. A namespace can also contain
other namespaces, forming a namespace hierarchy.
A Basic C# Program.
using System; // Import the System namespace
Data Types:
Apart from these basic types, C# also support ‘string’ data type to store multiple characters. i.e., a series of
characters or text. String values must be enclosed in double quotations.
string introduction = "Welcome to OOP Lab"; Console.WriteLine(introduction);
Comments in C#
A comment is text that is ignored by the compiler yet is beneficial to programmers. Normally, comments
are intended to mark code for future reference. They are treated as white space by the compiler. C# supports
two types of comments
• Single line comment – The // (two slashes) characters, followed by any character sequence. This
type of comment is terminated by a new line that is not immediately followed by a backslash.
• Multiple Line Comment – The characters /* (slash, asterisk), followed by any sequence of
characters (including new lines), followed by the characters */.
5
Object Oriented Programming
The first thing that you should notice when looking at this diagram is that the .NET Framework sits on top
of the operating system. Presently, the operating systems that can take the .NET Framework include
Window.
At the base of the .NET Framework is the Common Language Runtime (CLR). The CLR is the engine that
manages the execution of the code.
The next layer up is the .NET Framework Base Classes. This layer contains classes, value types, and
interfaces that you will use often in your development process. Most notably within the .NET Framework
Base Classes is ADO.NET, which provides access to and management of data.
The third layer of the framework is ASP.NET and Windows Forms. ASP.NET should not be viewed as the
next version of Active Server Pages after ASP 3.0, but as a dramatically new shift in Web application
development. Using ASP.NET, it’s now possible to build robust Web applications that are even more
functional than Win32 applications of the past.
The second part of the top layer of the .NET Framework is the Windows Forms section. This is where you
can build the traditional executable applications that you built with Visual Basic 6.0 in the past. There are
some new features here as well, such as a new drawing class and the capability to program these
applications in any of the available .NET languages.
6
Object Oriented Programming
Exercise:
1. Write a program that take input of your roll number along with the marks obtained in five subjects
and display the total marks obtained and the percentage.
7
Object Oriented Programming
2. Write a program to implement four function calculators. Allow user to provide input and select the
operation to be performed
8
Object Oriented Programming
9
Object Oriented Programming
10