14. Looping in C#
A loop repeats the same code several times. They make
calculations and processing elements in a collection
possible.
Types:
1.The for loop, which counts from one value to another.
2.The foreach loop, that easily iterates over all
elements in a collection.
3.The while loop, which goes on as long as some
condition is true.
4. The do-while loop, which always executes at least
once.
15. For Loop
▪ using System; Output:
0
▪ namespace MyApplication 1
2
▪ { 3
▪ class Program 4
▪ {
static void Main(string[] args)
▪ {
for (int i = 0; i < 5; i++)
▪ {
▪ Console.WriteLine(i); } }}}
16. Output:
0
using System; 1
namespace MyApplication 2
{ 3
class Program 4
{
static void Main(string[] args)
{
int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;}}}}
While Loop
17. The Do/While Loop
▪ using System; Output 0
▪ namespace MyApplication 1
▪ { 2
▪ class Program 3
▪ {static void Main(string[] args)
▪ {
▪ int i = 0;
▪ do
▪ {
▪ Console.WriteLine(i);
▪ i++; }
▪ while (i < 4);}}}}
18. The foreach Loop
▪ using System; Output: Toyota
namespace MyApplication BMW
▪ { Suzuki
▪ class Program Mazda
▪ {
▪ static void Main(string[] args)
▪ {
▪ string[] cars = {“Toyota", "BMW", “Suzuki", "Mazda"};
▪ foreach (string i in cars)
▪ {
▪ Console.WriteLine(i);
▪ } }}}
21. Object oriented program
▪ In oop,problem is divided into the number of grouos called objects
and then builds data and functions around these objects.
▪ It ties the data more closely to the functions that operate on it,and
protects it from accidental modifications from the outside functions.
▪ Communication of the objects done through function.
22. OBJECTS
▪ An object in OOPS is nothing but a self-contained
component which consists of methods and properties to make a
particular type of data useful.
▪ For example color name, table, bag, barking.When you send a
message to an object, you are asking the object to invoke or execute
one of its methods as defined in the class
23.
24.
25. Object…….
▪ When a program is executed, the objects interact by sending
messages to one another.
▪ For e.g if customer and account are two objects in a program , then
the customer objects may send a message to the account object
requesting for the bank balance.
▪ Each object contains data, and code to manipulate the data.
26. classes
▪ Classes are user‐defined data types and it behaves like built in types
of programming language.
▪ • Object contains code and data which can be made user define
type using class.
▪ • Objects are variables of class.
27.
28.
29.
30.
31.
32.
33.
34.
35. Inheritance
▪ Using inheritance you can create a general class that defines traits
common to a set of related items.
▪ This class can then be inherited by other, more specific classes, each
adding those things that are unique to it. In the language of C#, a
class that is inherited is called a base class.
45. ▪ Using system
▪ Namespace oops
▪ {
▪ Class program
▪ {
▪ Public void main function ()
▪ {
▪ Console.write line(“ main class”);
▪ }
▪ Static void main (string []args)
▪ {
▪ // Main class instance
▪ Program obj = new
Obj .main function ();
// Other class instance Program obj= new program ()
Obj . Main function ()
Other class instance
Demo d obj= new demo();
D obj.addition();
}
Class demo
{
Intx=10;
Int y=20;
Int z;
Public void addition
Z=x+y
Console.write line (“ other class is name space”);
Console.write line(z);
}
}
}
48. Data Types in C#:-
▪ A data type specifies the size and type of variable values.
▪ It means we must declare the type of a variable that indicates the
kind of values it is going to store, such as integer, float, decimal,
text, etc.
49. Data Types in C#:-
DataType Size Description
int 4 bytes Stores whole numbers from -2,147,483,648 to
2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808.
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7
decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal
digits
bool 1 bit Stores true or false values
char 2 bytes Stores a single character/letter, surrounded by single
quotes
string 2 bytes per
character
Stores a sequence of characters, surrounded by double
quotes
50. C# Classes and Objects:-
▪ Everything in C# is associated with classes and objects, along with its
attributes and methods. For example: in real life, a car is an object.The car
has attributes, such as weight and color, and methods, such as drive and
brake.
▪ A Class is like an object constructor, or a "blueprint" for creating objects.
▪ Create a Class
▪ To create a class, use the class keyword:
▪ Create a class named "Car" with a variable color:
▪ class Car
▪ {
▪ string color = "red";
▪ }
51. Code:-
▪ float floatVar = 10.2f;
▪ char charVar = 'A';
▪ bool boolVar = true;
▪ Console.WriteLine(stringVar);
▪ Console.WriteLine(intVar);
▪ Console.WriteLine(floatVar);
▪ Console.WriteLine(charVar);
▪ Console.WriteLine(boolVar);
▪ }
▪ }
Output:-
HelloWorld!!
100
10.2
A
True
▪ using System;
▪ public class Program
▪ {
▪ public static void Main() {
▪ string stringVar =
"HelloWorld!!";
▪ int intVar = 100;
52. Objects:-
▪ An object is created from a class. We have already created the class named Car, so now we can use this to
create objects.
▪ To create an object of Car, specify the class name, followed by the object name, and use the keyword new:
▪ using System;
▪ namespace MyApplication
▪ { output : red
▪ class Car
▪ {
▪ string color = "red";
▪ }
▪ sssCar myObj = new Car();
▪ Console.WriteLine(myObj.color);
▪ }
61. using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
int i = 0 ;
int item = 0 ;
int pos = 0 ;
int[] arr = new int[5]; //Read numbers into array
Console.WriteLine("Enter elements : ");
for (i = 0; i < arr.Length; i++) {
Console.Write("Element[" + (i + 1) + "]: ");
arr[i] = int.Parse(Console.ReadLine());
}
62. Console.Write("Enter item to search : ");
item = int.Parse(Console.ReadLine()); //Loop to search element in array.
for (i = 0; i < arr.Length; i++) {
if (item == arr[i]) {
pos = i + 1;
break; } }
if (pos == 0) {
Console.WriteLine("Item Not found in array");
}
else
{
Console.WriteLine("Position of item in array: "+pos); } } } }
64. “
The Array.Find() method searches for an element that
matches the specified conditions using predicate
delegate, and returns the first occurrence within the
entire Array.”
Syntax:
public static T Find<T>(T[] array, Predicate<T> match);
Array.Find()
65. string[] names = {"Steve", "Bill", "Bill Gates", "Ravi",
"Mohan", "Salman", "Boski" };
var stringToFind = "Bill";
var result = Array.Find(names, element => element ==
stringToFind);
output// returns "Bill" - 3
Find literal value
66. string[] names = { "Steve", "Bill", "Bill Gates",
"James", "Mohan", "Salman", "Boski" };
var result = Array.Find(names, element =>
element.StartsWith("B"));
output// returns Bill
Find elements that starts with B
67. string[] names = { "Steve", "Bill", "James", "Mohan",
"Salman", "Boski" };
var result = Array.Find(names, element =>
element.Length >= 5);
output// returns Steve
Find by length
68. “The Array.FindAll() method returns all elements that match
the specified condition.”
Syntax:
public static T[]
FindAll<T>(T[] array, Predicate<T> match)
Array.FindAll()
69. string[] names = { "Steve", "Bill", "bill", "James",
"Mohan", "Salman", "Boski" };
var stringToFind = "bill";
string[] result = Array.FindAll(names, element =>
element.ToLower() == stringToFind);
output// return Bill, bill
Find literal values
70. string[] names = { "Steve", "Bill", "James", "Mohan",
"Salman", "Boski" };
string[] result = Array.FindAll(names, element =>
element.StartsWith("B"));
output// return Bill, Boski
Find all elements starting with B
71. string[] names = { "Steve", "Bill", "James", "Mohan",
"Salman", "Boski" };
string[] result = Array.FindAll(names, element =>
element.Length >= 5);
output// returns Steve, James, Mohan, Salman, Boski
Find elements by length
72. “The Array.Find() method returns the first element
that matches the condition.
The Array.FindLast() method returns the last element
that matches the specified condition in an array.”
Syntax:
public static T FindLast<T>(T[] array, Predicate<T>
match)
Array.FindLast()
73. string[] names = { "Steve", "Bill", "Bill Gates", "Ravi",
"Mohan", "Salman", "Boski" };
var stringToFind = "Bill";
var result = Array.FindLast(names, element =>
element.Contains(stringToFind));
output // returns "Boski"
Find last element