![]() |
| | #1 |
| Banned Join Date: Oct 2001
Posts: 1,784
| Does C# have pointers |
| Troll_King is offline | |
| | #2 |
| Sénior Member Join Date: Jan 2002
Posts: 982
| No user objects are passed by reference by default. Here's a very quick linked list (This probably doesn't represent the best way of doing it as I'm not up to full speed on C# myself) - Code: using System;
namespace ConsoleApplication1
{
class Node
{
public int x;
public Node next;
public Node (int _x)
{
x=_x;
}
}
class List
{
private Node head;
private Node sentinel;
public List(int x)
{
head = new Node(x);
sentinel=new Node(0);
head.next=sentinel;
}
public void Push_Back(int x)
{
Node temp = head;
while(temp.next.x!=0)
temp=temp.next;
temp.next = new Node(x);
temp.next.next=sentinel;
}
public void Print()
{
Node temp=head;
while(temp.x!=0)
{
Console.WriteLine(temp.x);
temp=temp.next;
}
}
}
class Class1
{
static void Main(string[] args)
{
List l = new List(1);
l.Push_Back(2);
l.Push_Back(3);
l.Print();
}
}
}
|
| Sorensen is offline | |
| | #3 |
| Banned Join Date: Oct 2001
Posts: 1,784
| I noticed something similar to this where the class name was used instead of the pointer. If objects are passed by reference as a default than maybe it makes more sense. Not used to it though. |
| Troll_King is offline | |
| | #4 |
| Sénior Member Join Date: Jan 2002
Posts: 982
| You can actually use pointers if you really want to, by marking code as unsafe (and setting a command line flag). |
| Sorensen is offline | |
| | #5 |
| the hat of redundancy hat Join Date: Aug 2001 Location: Hannover, Germany
Posts: 2,768
| There are two types of objects in C#: Value types and reference types. Value types are all primitive datatypes like int and double as well as all structs (!). Reference types are all classes. Reference types are always passed by reference, while valuetypes are always passed by value. Calling a new operator and therefore constructor on a reference type creates a new object initialized by it's contructor. Calling new on a valuetype creates no new object. It just calls the contructor on the already existing object. Example: PHP Code: Example 1 The following example uses pointers to copy an array of bytes from src to dst. Compile the example with the /unsafe option. PHP Code: The first 10 elements are: 0 1 2 3 4 5 6 7 8 9 Code Discussion Notice the use of the unsafe keyword, which allows pointers to be used within the Copy method. The fixed statement is used to declare pointers to the source and destination arrays. It pins the location of the src and dst objects in memory so that they will not be moved by garbage collection. The objects will be unpinned when the fixed block completes The reason why unsafe code is beneficial here is that it allows copying the array in chunks of 4 bytes at a time, and directly by incrementing pointers, thereby getting rid of array bounds checks and address calculations in each iteration. Hope that satisfies your hunger for code
__________________ hth -nv She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate." When in doubt, read the FAQ. Then ask a smart question. |
| nvoigt is offline | |
| | #6 |
| Banned Join Date: Oct 2001
Posts: 1,784
| //c sharp version Code: int [] csharparray = new int [10];
for(int i =0; i < csharparray.Length; i++)
{
csharparray[i] = new int;
}
Code: int *carray; carray = (int *) malloc (sizeof(int) * 10); |
| Troll_King is offline | |
| | #7 |
| the hat of redundancy hat Join Date: Aug 2001 Location: Hannover, Germany
Posts: 2,768
| >csharparray[i] = new int; this does not create a new int. csharparray[i] already is an int, because integers are valuetypes. Calling the constructor just sets it back to value zero. For objects of referencetype ( classes ) this loop is ok, for valuetypes, it's superfluent. C# Version: int [] iArray = new int[10]; C++ Version: int* iArray = new int[10]; C Version: int* iArray = (int*)calloc( sizeof(int), 10 );
__________________ hth -nv She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate." When in doubt, read the FAQ. Then ask a smart question. |
| nvoigt is offline | |
| | #8 | |
| Banned Join Date: Oct 2001
Posts: 1,784
| Quote:
| |
| Troll_King is offline | |
| | #9 |
| the hat of redundancy hat Join Date: Aug 2001 Location: Hannover, Germany
Posts: 2,768
| For classes ( reference types in C# ) the loop is the right solution. C# Version: Hashtable [] hArray = new Hashtable[10]; for( int i = 0 ; i < 10 ; i++ ) hArray[i] = new Hashtable(); C++ Version: Hashtable* hArray = new (Hashtable*)[10]; for( int i = 0 ; i < 10 ; i++ ) hArray[i] = new Hashtable();
__________________ hth -nv She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate." When in doubt, read the FAQ. Then ask a smart question. |
| nvoigt is offline | |
| | #10 |
| Sénior Member Join Date: Jan 2002
Posts: 982
| >Is it just okay than or is it a necessity to allocate space that way. First for the array as a whole, than for each element. Doing it this way, it would appear so. Just doing - myclass [] Array = new myclass[10]; doesn't call a constructor, and trying to call a myclass method without further allocation using a loop results in a NullReferenceAllocation. Alternatively an array can be allocated like - myclass [] Array = new myclass[2]{new myclass(),new myclass()}; or myclass [] Array = new myclass[]{new myclass(),new myclass()}; or myclass [] Array = {new myclass(),new myclass()}; |
| Sorensen is offline | |
| | #11 | |
| Banned Join Date: Oct 2001
Posts: 1,784
| I mostly come from a C background and this to me looks sufficient: Quote:
| |
| Troll_King is offline | |
| | #12 |
| Guest
Posts: n/a
| Also, C# syntax looks a lot like Java's Mr. c. |
|
| | #13 |
| Guest
Posts: n/a
| I think it looks a little better than Java's syntax though. |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Using pointers to pointers | steve1_rm | C Programming | 18 | 05-29-2008 05:59 AM |
| function pointers | benhaldor | C Programming | 4 | 08-19-2007 10:56 AM |
| Request for comments | Prelude | A Brief History of Cprogramming.com | 15 | 01-02-2004 10:33 AM |
| Staticly Bound Member Function Pointers | Polymorphic OOP | C++ Programming | 29 | 11-28-2002 01:18 PM |