الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

ماهي وظيفة Operator في C#

بدأه MKSoft في 24 أبريل 2008 · 4 رد · 1,594 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

ارجو منكم اخواني ان تفسرو لي هذه التقنية الجديدة operator و الموجودة في Version 2 .NET و الموجودة في هذا الكود.

public static Cycle operator +(Cycle arg1,int arg2)
		{
			arg1.Value += arg2;
			return arg1;
		}
		public static Cycle operator -(Cycle arg1, int arg2)
		{
			arg1.Value -= arg2;
			return arg1;
		}

وأيضا هذا الإستدعاء لهذه التقنية من خلال الكود التالي.

Cycle degrees = new Cycle(0, 359);
			Cycle quarters = new Cycle(1, 4);
			for (int i = 0; i < 800; i++)
			{
				degrees += 90;
				quarters += 1;
}

هذا الكود بشكل كامل , للتوضيح أكثر .

struct Cycle
	{
		int val, min, max;
		Cycle(int min,int max)
		{
			val = min;
			this.min = min;
			this.max = max;
		}
		public int Value
		{
			get{return val;}
			set{if (value > max){
				val = min; }
				else {if (value < min)
						val = max;
					else
						val = value;}}}
		public int ToInteger()
		{return Value;}
		public override string ToString()
		{return Value.ToString();}
		public static Cycle operator +(Cycle arg1,int arg2)
		{
			arg1.Value += arg2;
			return arg1;
		}
		public static Cycle operator -(Cycle arg1, int arg2)
		{
			arg1.Value -= arg2;
			return arg1;
		}

		static void Main(string[] args)
		{
			Cycle degrees = new Cycle(0, 359);
			Cycle quarters = new Cycle(1, 4);
			for (int i = 0; i < 800; i++)
			{
				degrees += 90;
				quarters += 1;
				Console.WriteLine("degrees = {0} , quarters = {1}", degrees, quarters);
			}

		}
#2

الله أعلى و أعلم من الجميع

#4

احب ان اضيف اننا يمكننا استخدامها في إعادة تعريف دالة لاستخدامها في جمع كائنين من عدة كلاسات مختلفة

وكما قال اخي السابق التحميل الزائد over loadin سواء كان جمع او ضرب او قسمة

#5

احب أن أضيف أنها ليست تقنية جديدة بل هي موجودة منذ أول نسخة من ال#C

Sr. Software Development Engineer
Hulu, LLC
My Blogs

مواضيع مشابهة