Lab 04 - Classes and Object C#
Lab 04 - Classes and Object C#
Objectives
● To learn about the C# Programming Language and general syntax along with Classes and
Objects.
Namespaces
To make the .NET Framework more manageable, Microsoft has given it a hierarchical structure. This
hierarchical structure is organized into what are referred to as namespaces. By organizing the
framework into namespaces, the chances of naming collisions are greatly reduced. Organizing related
functionality of the framework into namespaces also greatly enhances its usability for developers. For
example, if you want to build a window’s GUI, it is a pretty good bet the functionality you need exists in
the:
"System.Windows" namespace.
Class
A class is considered as a construct which encapsulates a group of variables and methods.
(Definition from book) In OOP, class is called a definition (specification) of a given type of objects from
the real-world. The class represents a pattern, which describes the different states and behavior of
certain objects (the copies), which are created from this class (pattern).
Object
In OOP terms, an object is an instance of class for incorporating data and the procedures for working
with that data.
(Definition from book) An object is a copy created from the definition (specification) of a given class, also
called an instance. When one object is created by the description of one class we say the object is from
type "name of the class".
// functions
public void functionName {
// function body
}
// member variables
private string message;
}
Example 4.1
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello C#!");
System.Console.ReadLine();
}
}
}
Output
Hello C#!
Note: Here we introduce System.Console.ReadLine() so that terminal after displaying output does not
get close. We will see other ways of compiling also.
char userOption;
System.Console
.WriteLine("Input character to check if it is a vowel or not: ");
userOption = System.Console.ReadLine()[0];
Output
Sum of two numbers is: 56
Area of Circle is: 78.7
Input character to check if it is a vowel or not:
a
Input Character is vowel: True
string
A string is a sequence of characters stored in a certain address in memory. In the variable of type char
we can record only one character. Where it is necessary to process more than one character then strings
come to our aid.
The System.String Class
The class System.String e nables us to handle strings in C#. For declaring the strings we will continue
using the keyword string, which is an alias in C# of the System.String class from .NET Framework. The
work with string facilitates us in manipulating the the text content: construction of texts, text search and
many other operations.
We have just declared the variable greeting of type string whose content is the text phrase "Hello, C#".
The representation of the content in the string looks closely to this:
H e l l o , C #
Question:
What is the use of string class when we can use char array?
System.Console.WriteLine();
System.Console.WriteLine();
System.Console.WriteLine(quote);
System.Console.WriteLine();
System.Console.WriteLine();
Output
message = This is a sample string message.
message.Length = 31
message[0] = T
message[1] = h
message[2] = i
message[3] = s
....
Book's title is "Intro to C#"
Enter any string:
DHA Suffa University
You entered: DHA Suffa University
More on string
word1.CompareTo(word2) If you want to compare two words and get information which one
of them is before the other according to their alphabetical order
of letters
str1.IndexOf(str2); Returns the starting index of str2 in str1 if present, else returns
-1
str.Substring(startIndex, Returns substring of str from start index to start index + number
numberOfChars); of characters specified
Home Assignment
Task 01:
Find if given string is a palindrome or not.
Str = level
Output = Palindrome
Task 02:
Print duplicate characters from a String.
Str = this is a sample text
Output = isate
Task 03:
Print all substrings of a given String.
Str = suf
Output:
s
su
suf
u
uf
f
Task 04:
Reverse words in a string (It is different from printing string in reverse).
Str = hello world
reverseStr = dlrow olleh
Submission Instructions
1. Number your solution folders as question number e.g. Q1, Q2, etc. (Q is in upper case)
2. Every folder should contain single .cs file
3. Create a new folder named cs152abc where abc is your 3 digit roll #. e.g. cs152111.
4. Copy all the project folders into this folder.
5. Now make sure a zip file named cs152abc.zip is created e.g. cs152111.zip
6. Upload the assignment solution on LMS under the assignment named Lab 04