Professional Documents
Culture Documents
NET
ולסיום -פרויקט
כללים
השעור מתחיל בזמן ,עם כל מספר של תלמידים
הפסקה – 15דקות בלבד .נא לא ללכת לסופר.
עבודה תנתן כל שבוע ביום ד .הגשה ביום ב.
חומר לקריאה באנגלית ,ינתן מידי יום ב'.
חובה לבצע את כל התרגילים.
יותר תרגול בבית – יותר הספק.
First Program
File: helloWorld.cs
class HelloWorld
{
public static void Main))
{
System.Console.WriteLine)"Hello, World");
}
}
http://blogs.wdevs.com/Gootvilig
יעדי פיתוח
חלונות
WEB
מחשבי כף יד
טלפונים ניידים
...
הדגמות
CUIFirst Program תוכנית
GUIHelloGUI.cs תוכנית
WEB תוכנית
HelloGUI.cs
using System.Windows.Forms;
using System.Drawing;
class MyForm:Form
{
public static void Main))
{
Application.Run)new MyForm)));
}
הדגמה
Demo
דוגמאות בסיסיות עם IO
WelcomeCSS.cs
// Namespace Declaration
using System;
// Program start class
class WelcomeCSS
{
// Main begins program execution.
public static void Main()
{
// Write to console
Console.WriteLine("Welcome to the C#!");
}
}
InteractiveWelcome.cs
using System;
byte 8 0 to 255
ushort 16 0 to 65535
uint 32 0 to 4294967295
ulong 64 0 to 18446744073709551615
char 16 0 to 65535
The Floating Point and Decimal Types with
Size, Precision, and Range
Type Size (in bits) Precision Range
class Queue
{...
public void Enqueue)object x) {...}
public object Dequeue)) {...}
{
This Queue can then be used for reference types and value types
q.Enqueue)new Rectangle)));
q.Enqueue)3);
לולאות
foreach statement –
more examples
More about foreach
private static void Main)string[] args)
static void Main)string[] args)
{
{ int[] numArray1 = new int[] { 1, 2, 3 };
int[] numArray2 = numArray1;
int [] intArray = new int[] {1,2,3};
for )int num4 = 0; num4 < numArray2.Length;
foreach)int i in intArray) num4++)
{
Console.Write)"{0} ",i); int num1 = numArray2[num4];
Console.Write)"{0} ", num1);
}
Queue q = new Queue)); Queue queue1 = new Queue));
foreach)int i in intArray) int[] numArray3 = numArray1;
for )int num5 = 0; num5 < numArray3.Length;
q.Enqueue)i); num5++)
{
int num2 = numArray3[num5];
foreach)int i in q) queue1.Enqueue)num2);
Console.Write)"{0} ",i); }
foreach )int num3 in queue1)
{
Console.ReadLine)); Console.Write)"{0} ", num3);
}
} Original From Reflector
Console.ReadLine));
}
String & Array
using System;
class ForEachLoop
{
public static void Main))
{
string[] names = {"Cheryl", "Joe", "Matt", "Robert"};
// Array of references
SomeClass[] d = new SomeClass[10];
int x = a[0][1];
len = a.GetLength)0); // 2
len = a.GetLength)1); // 3
Enumeration
Declaration )directly in a namespace)
enum Colors {red, blue, green} // values: 0, 1, 2
enum Access {personal=1, group=2, all=4}
enum Access : byte{personal=1, group=2, all=4}
Use
Colors c = Colors.blue; // must be qualified
Access a = Access.personal | Access.group;
if ))Access.personal & a( != 0(
Console.WriteLine)"access granted"(;
.Enums – cont
Strongly typed
Noimplicit conversions to/from int
Operators: +, -, ++, --, &, |, ^, ~
Can specify underlying type
Byte, short, int, long
enum Color: byte
{
Red = 1,
Green = 2,
Blue = 4,
Black = 0,
White = Red | Green | Blue,
}
using System;
Dll and Exe
public class Fib{
Decimal current;
Decimal last;
using System;
public Fib)){
current = 1; class App
last = 0; {
} public static void Main))
{
private Fib)Decimal last, Decimal Int32 index = 50;
secondToLast){
current = last+secondToLast;
Fib obj = new Fib));
this.last = last; do{
} Console.WriteLine)obj.Value);
obj = obj.GetNext));
public Fib GetNext)){ }while)index-- != 0);
return new Fib)current, last); }
} }
public Decimal Value{
csc /r:FibObj.dll FibTest.cs
get{return current;}
}
}
DEMO
Improve the DrawingObject
Add
Color
Rectangle )Think how to implement in Line)
Add
Resize)int percent);
Draw object after changes take place.
readonly
public string readonly Name = “Yossi”;
Or
public string readonly Name;
public Person))
{
Name = “Shlomi”;
}
const
class MyClass
{
public string ErrorMessage = “Error”;
public void DoNothing))
{
const int c = 770;
Console.WriteLine)“Loacl constant = {0}”,c);
}
Exceptions
static void Main)string[] args)
{
try
{
File.OpenRead)"NonExistentFile");
}
catch)Exception ex)
{
Console.WriteLine)ex.ToString)));
}
}
System.IO.File.OpenRead)(
Exceptions
SecurityException
ArgumentException
ArgumentNullException
PathTooLongException
DirectoryNotFoundException
UnauthorizedAccessException
FileNotFoundException
NotSupportedException
catch order
catch)FileNotFoundException fnfex)
{
Console.WriteLine)fnfex.ToString)));
}
catch)Exception ex)
{
Console.WriteLine)ex.ToString)));
}
try – catch - finally
FileStream outStream = null; FileStream inStream = null;
try
{
outStream = File.OpenWrite)"DestinationFile.txt");
inStream = File.OpenRead)"BogusInputFile.txt");
}
catch)Exception ex)
{ Console.WriteLine)ex.ToString))); }
finally
{
if )outStream != null)
{
outStream.Close));
}
if )inStream != null)
{
inStream.Close));
}
}
Communication between object
interface
delegate
event
interface
Interface ISon
{
void Feed)Food food);
}
class Father
{
ISon [] sons = new sons [5];
void DoWork))
{
Food food = new Food)5);
foreach)ISon son in sons)
son.Feed)food.One);
}
}
delegate & event
Demonstration in SSW 2 – p21
g e
an
on C h
Lig h
htC i tc
ha Sw
ng
e on
Click
Switch
Homework
Add enum of priority to the ToDoList
Get as a string from the user, parse be
Enum.Parse)).
Use try-catch to avoid exceptions
Advanced – print sorted by priority.
)you can try to use System.Collections.
Not must.)
Object Interface
) )Equals .Evaluates whether two objects are equivalent
SSW1 – 63/4
DEMO : ObjectInterface
String
DEMO
Indexers
public class ListBox: Control
{
private string[] items;
More – SSW 1 - 61
System.String Indexer
// Properties
int Count { get; }
bool IsSynchronized { get; }
object SyncRoot { get; }
}
IList
public interface IList : ICollection, IEnumerable
{
// Methods
int Add)object value);
void Clear));
bool Contains)object value);
int IndexOf)object value);
void Insert)int index, object value);
void Remove)object value);
void RemoveAt)int index);
// Properties
bool IsFixedSize { get; }
bool IsReadOnly { get; }
object this[int index] { get; set; }
}
Collections
Lists
Array
ArrayList
StringCollection
Queue
Stack
BitArray
CollectionBase
ReadOnlyCollectionBase
IDictionary
public interface IDictionary : ICollection, IEnumerable
{
// Methods
void Add)object key, object value);
void Clear));
bool Contains)object key);
IDictionaryEnumerator GetEnumerator));
void Remove)object key);
// Properties
bool IsFixedSize { get; }
bool IsReadOnly { get; }
object this[object key] { get; set; }
ICollection Keys { get; }
ICollection Values { get; }
}
Dictionaries
SortedList
Hashtable
ListDictionary
HybridDictionary
StringDictionary
DetionaryBase
Sort Arrays
Array.Sort
Sorts the elements in an entire one-dimensional
Array using the IComparable interface
implemented by each element of the Array
Array.BinarySearch
Searches a one-dimensional sorted Array for a
value, using a binary search algorithm.
IComparable Interface
CompareTo
Compares the current instance with
another object of the same type.
Classes already implement IComparable
String
Enum
Primitive types
public class Temperature : IComparable
{
public int CompareTo)object obj)
{
if)obj is Temperature)
{
Temperature temp = )Temperature) obj;
return m_value.CompareTo)temp.m_value);
}
throw new ArgumentException)"object is not a Temperature");
}