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

Pointers in C#.NET

مغلق
بدأه jafar_pal في 26 مارس 2006 · 19 رد · 1,953 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

Hi every body

I'm going to do a homewrok that requists to make a linked list in C# (I know that there is a pre-defined linked list in C# but it's forbidden to use it, I must create my own one)

I searched in MSDN for .NET and I found that there are pointers in C# ,but the compiler requised that the function must be <unsafe> what is the unsafe property

and how can Iuse a pointer like that in C language ,I need the pointer to point to a class defined by me.

plz help me quickly

sorry for writing in english ,but the PC I use now has no arabic

تم تعديل هذه المشاركة بواسطة HGB في 29 مارس 2006 في 20:06

#2

طريقة عمل LinkedList :

public class Node
{
     //My Variables
      protected int anything;  

      // pointer to next Node;
      protected Node nextNode;      
}

public class myClass{

     private Node n;

     public myClass(){
             n = new Node();
             n.anything = 5;
             n.nextNode = null;
            // or n.nextNode = new Node();
            // n.nextNode.anything .........
       }
}

أما اذا أردت ان تستخدم ال unsafe :

unsafe struct Node{
      int x;
      Node* n;
}

ولكن عليك ان تقوم بتغيير اعدادات ال Project حتى يتمكن من عمل compile لل unsafe code

#3

شكرا أخي baha_ashour على هذه الإيضاحات، ولكن يوجد عندي نقطة غامضة و هي بخصوص unsafe code

أنا أتوقع أنه نمط خاص للـpointers و لكن ما هي الإعدادات الخاصة به

#4

1- اذهب الى ال Properties الخاصة ب ال Project .

2- اذهب الى ال Bulid

3- اعمل check ل allow unsafe code

#5

السلام عليكم

you don't need pointers for this case, you can use ordingary reference to ojects.

#6
اقتباس
1- اذهب الى ال Properties الخاصة ب ال Project .

2- اذهب الى ال Bulid

3- اعمل check ل allow unsafe code

شكرا اخي ولكن ماذا تقصد بـ unsafe code؟؟

عندما تولد يا ابن آدم يؤذن في اذنك من غير صلاة...

وعندما تموت.... يصلى عليك من غير آذان....

فكأن حياتك في الدنيا ليست سوى... الوقت الذي تمضيه بين الأذان والصلاة...

فلا تقضيه بما لاينفع

Mohammad Lahlouh

mohammadlahlouh.blogspot.com

Make money from your Website or Blog with BidVertiser

#7

أخي بهاء شكرا كثير على الرد

بالنسب للكود الاول

كيف نربط الـ Nodes معا؟؟

ما فهمت :unsure: :unsure: :unsure:

My_Sign.jpg
#8

أعتقد أن المثال واضح أخي ابراهيم :blink:

اليك مثال آخر ;) :

public class myClass{

    private Node root;

    public myClass(){
            // my First Node (the root)
            root = new Node();
            root.anything = 5;
           
           //create another node
            Node anotherNode = new Node();
            anotherNode .anything =6;
            anotherNode .nextNode = null;

            //link this node to the first Node "the root"
            root.nextNode = anotherNode;
      }
}
#9

أعتقد اني فهمت الفكرة :rolleyes:

شكرا م. بهاء ;)

My_Sign.jpg
#10

أخي بهاء وإخواني في المنتدى العربي......واجهتني مشاكل كثيرة في البرمجة عندما أردت تحويل كود يحتوي على pointers من ++C إلى #C

إليكم جزءا بسيطا من الكود

هنا لا مشاكل

	unsafe struct NODE 
 	 {
    string name;
    string address
    //other data
    NODE * next;
 	 };

لكن هنا المشاكل تبدأ

public unsafe NODE * create_node_with_data(string fName, string addr)
{
	NODE *n;
	n=new NODE;
	n->next=NULL;
	n->name=fName;
	n->address=addr;
	return n;
}

طبعا الكود الأخير كما هو واضح يقوم بإنشاء NODE جديدة ويضع داخلها المعلومات المناسبة ثم يقوم بإرجاع هذه الـNODE حتى يتم ربطها مع الـNODES الأخرى من خلال method خاصة بذلك................

تكمن المشكلة في السطر الاول: "public unsafe NODE * create_node_with_data"

Inconsistent accessibility: return type 'TestApp.NODE*' is less accessible than method 'TestApp.Linked.create_node_with_data(string, string)'

أرجوا منكم المساعدة لأن pointers هي الأساس في كل ما أقوم به الآن..................

My_Sign.jpg
#11

يجب أن تعرف ال structure وال variables اللي فيها ... public

unsafe public  struct NODE 
  {
   public string name;
   public string address
   //other data
   public NODE * next;
  };

تحياتي ;)

#12

للأسف الشديد ما زالت المشكلة موجودة نفسها.......... :blink: :blink: :blink:

My_Sign.jpg
#13

آه عفوا :s

لم انتبه على سؤالك جيدا

المشكلة ان NODE *n معرفة داخل ال Function

وبعد الانتهاء من تنفيذ ال Function سوف يتم حذفها من ال memory

اذأ ... كيف تريد ارجاع pointer عليها

لذلك ... يجب ان تكون معرفة public

وعلى ما يبدو ... يجب أن تكون خارج ال method

:s

#14

لكن أعتقد أنه إذا عرفناها خارج الـ method فإننا سنتعامل مع نفس النسخة كلما عملنا creation لـواحدة جديدة............

آسف إذا غلبتك معي أخي بهاء.............. :^) :^) :^)

My_Sign.jpg
#15

والله يا استاذ ابراهيم اللي فهمتوا وزبط معي .. هو كالتالي:

public unsafe struct Node
{
        public int x;
        public Node* Left;
}

private unsafe Node CreateNode()
{
            Node n = new Node();
            n.x = 0;
            n.Left = null;

            return n;
}

لا أعتقد انك تستطيع عمل Node * n في ال #C

:unsure:

انا أصلا تعلمت ال #C لأني أكره ال pointers كثييييييييييييرا

:D

تم تعديل هذه المشاركة بواسطة baha_ashour في 30 مارس 2006 في 23:17

#16
Muhammad Adel كتب:
السلام عليكم

you don't need pointers for this case, you can use ordingary reference to ojects.

بالظبط

لا أدري لماذا أنت مصر على إستخدام pointers بينما يمكنك إستخدام ال references العادية الخاصة بالدوت نيت

لإن أي object تم إنشائه من Class في سي شارب هو reference و يمكنك إستخدامه بدلا من الpointers

بينما أي object تم إنشائه من Value Type يعني Structure هو متغير عادي value و ليس reference

#17

ماشي يا شباب...................مشكورين كثير على ردودكم وتفاعلكم

وشكر أيضا للأخ بهاء.................انا ما بحب pointer لكن مضطر أستخدمها.......

سأستخدم Reference وأحاول,,,,,,,,,,

My_Sign.jpg
#18
public unsafe NODE * create_node_with_data(string fName, string addr)
{
NODE *n;
n=new NODE;
n->next=NULL;
n->name=fName;
n->address=addr;
return &n;
}

وبكدة ممكن الكود يشتغل لية؟؟

المؤشرات متهى إلا متغيرات بتيشل عناوين وليس قيم مباشرا وانتا عاوز ترجع من الدالة Value type وتخزنة داخل pointer وإحنا عارفين أن المؤشرات ماهى إلا متغيرات بتشيل عناوين وبالتالى لازم ترجع عنوان المتغيرn وليس القيمة بذتها

أو ممكن تستخدم الreferences كما قال الاخوة وأعتذر عن التأخر فى الرد :)

تم تعديل هذه المشاركة بواسطة eng_3llam في 1 أبريل 2006 في 21:12

Muhammad Allam

Computer Science

@Resource(MappedURL="My Blog" )

#19

السلام عليكم

first: pointers can't point to an object but can point to one field in this object,and in this case you should set this object to be fixed -fixed is keyword- because Garbage Collector Reallocate objects -managed the heap-

seconed: surround this code by unsafe block why?because pointers not managed code

i hope that i add new

#20
اقتباس
first: pointers can't point to an object but can point to one field in this object,and in this case you should set this object to be fixed -fixed is keyword- because Garbage Collector Reallocate objects -managed the heap

تمام يا أخ أحمد لان اى object يخزن داخل الmanage heap ونظريا ممكن pointer يشير الى object أتعمل لة Clean ولكن عمليا عمر ما الcompiler هيسمح بذلك وبالتالى علشان أشير الى كائن عن طريق مؤشر أستخدم الFixed Keyword كما موضح

class Point
    {
        public int x, y;
    }

//In Main
            Point p = new Point();
            p.x = 1;
            p.y = 2;

            unsafe
            {
                fixed (int* pointer = &p.x)
                { 
                    
                    *pointer = 3;
                    Console.WriteLine("the val is {0}",p.x); 
                }
            }

ولكن فى تلك الحالة مستخدم الstruct وهو عبارة عن Value Type :)

Muhammad Allam

Computer Science

@Resource(MappedURL="My Blog" )

هذا الموضوع مغلق.

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