עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

delegate C sharp

Featured Replies

פורסם

יש לי Class Point:


//1:define delegate class
public delegate string PointFormat(int x, int y);
public class Point:IComparable<Point>
{
public int X { get; set; }
public int Y { get; set; }
// public PointFormat format; //3:Allocate delegate reference


public Point(int x, int y)
{
this.X = x;
this.Y = y;
}
//public override string ToString()
//{
// if (format != null)
// {
// return format.Invoke(X, Y); //5:invoke
// }
// else
// {
// return "(" + X + "," + Y + ")";
// }
//}
public void Print(PointFormat format) //3: Allocate delegate reference
{
string text = format.Invoke(X, Y); //5:invoke
Console.WriteLine(text);
}


public int CompareTo(Point other)
{
return X * Y - other.X * other.Y;
}
}

ו-List של Points:


List<Point> PointsList = new List<Point>();
PointsList.Add(new Point(100, 200));
PointsList.Add(new Point(1, 1));
PointsList.Add(new Point(555, 555));
PointsList.Add(new Point(6, 6));

עליו אני צריך להגדיר אופרטור של חיבור (רק של ה-X'ים)

רשמתי מתודה:


static int Sum2Points(Point p1, Point p2)
{
return p1.X + p2.X;
}

אח"כ שאני רוצה להפעיל אותה אני רושם PointsList.sum ונפתח לי תפריט שבו אני רושם בסוגריים את שם המתודה שלי אבל הוא מבקש ממני עוד כמה דברים ודי הסתבכתי למרות שניסיתי.

פורסם

א. מה הקשר Delegate?

ב. אם אתה רוצה לעשות Sum של X בין 2 נקודות אז פשוט תעביר את הנקודות לפונקציה שכתבת, אני לא מבין מה ניסית לעשות.

אם מה שניסית לעשות זה לעשות SUM של X בכל הרשימה, אתה לא צריך שום פונקציה, פשוט:

int SumOfX = PointsList.Sum(P => P.X);

  • 2 שבועות מאוחר יותר...
פורסם

אם לא שמת לב זה בידיוק DELEGATE, פשוט כתיבה מקוצרת.

פורסם

זה לא delegate, זה lambda (יש הבדל בין השניים). אבל כל lambda יכול להפוך ל-delegate.

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.