Download presentation
Presentation is loading. Please wait.
1
Array
2
One Dimension Array <ArrayType> [ ] Arrayname = new <arrayType> [<size>]; int [ ] x =new int [3]; int [ ] x={9,7,6}; int [ ] x =new int [5] {3,4,5,6,7}; int [ ] x =new int [10] {3,4,5,6,7};
3
const arraysize=5 int [ ] myaaray=new int [arraysize] {6,8,7,8,8};
int [ ] x =new int [3]; x[0]=21; x[1]=63; x[2]=7; float [ ] f= new [2.5f, 5.5f, 3.5f] char [ ] ch ={'a', 'b', 'c'}
4
Console.WriteLine(<array name>.Length);
Ex: static void Main(string[] args) { string[ ] friendname ={ "huda", "saja", "noor" }; int i; Console .WriteLine ("Here are {0} of my friend:" , friendname .Length ); for (i =0; i <friendname .Length ;i++) Console .WriteLine (friendname [i]); }
5
Ex: static void Main(string[] args) { int[ ] x = new int[5]; for (int i = 0; i < 5; i++) { Console.Write("Enter value at location {0} in array :", i + 1); x[i] = int.Parse(Console.ReadLine()); } Console.WriteLine(); for (int j=0; j<5 ; j++) Console .WriteLine ("Value of location {0} is :{1}", j+1,x[j]); }
6
2. Two dimensional Array Jagged array Rectangular array
Type [ , ] name = new Type [size,size] int [ , ] x=new int [2,3] {{1,4,2},{52,12,9}}; int [ , ] x=new int [2,3]; x[0,0]=14;
7
double [ , ] distances =new double [4,4]
{200,800,70,400}, {800,400,120,490}, {300,620,890,590}}; -For (i=0; i<3; i++) For (j=0; j<3; j++) Console.writeline(x[i][j]);
8
foreach loops: foreach (<basetype> <name> in <array>) { // can use <name> for each element }
9
EX: static void Main(string[] args) { string[] friendnames ={ "huda", "saja", "noor" }; int i; Console.WriteLine("Here are {0} of my friend:", friendnames.Length); foreach (string friend in friendnames ) Console.WriteLine(friend); }
10
ان الفرق الرئيسئ بين استخدام هذه الطريقة عن استخدام for هو ان حلقة foreach تؤمن لنا وصولاً محمياً (للقراءة فقط) لمحتويات المصفوفة، أي لايمكننا تعديل محتويات المصفوفة ضمن الحلقة فلا يمكننا ان نكتب: foreach (string friend in friendnames ) { Friend ="suha"; }
11
static void Main(string[ ] args)
{ int[] x = new int[5] { 6, 7, 8, 2, 4 }; int sum = 0; foreach (int y in x) sum += y; Console.WriteLine(sum); }
12
Class -Class : It is the basic unit of the OOP which is used to wrap up(تغليف) the data and the codes (encapsulation). - The code and data that form the class are called the members of the class. - A class is defined as a data structure used to create object. Declare a class using the class keyword.
13
The syntax is as follows:
[access-modifiers] class <classname> [:base-class [,interface(s)]] { class-body //It contents field, properties, methods, event(members) }
14
Member Definitions All members have their own accessibility level, defined in all cases by one of the following keywords: public: Member is accessible from any code private: Member is accessible only from code that is part of the class (the default if no keyword is used) protected: Member accessible only from code that is part of either the class or a derived class
15
Example 1 : using System; using System.Collections.Generic;
using System.Text; public class Time { private int Year; private int Month; private int Date; private int Hour; private int Minute; private int Second; public void DisplayCurrentTime( ) { System.Console.WriteLine( "{0}/{1}/{2} {3}:{4}:{5}", Month, Date, Year, Hour, Minute, Second ); }
16
static void Main( ) { Time t = new Time( ); t.DisplayCurrentTime( ); }
17
Object Creating Objects Time t = new Time( );
- An instances of a class are called objects (instantiated). - Objects are created in memory when your program executes. Creating Objects -Objects can be created using the new keyword followed by the name of the class that the object will be based upon, like this: Time t = new Time( );
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.