C # pointer alternative for C ++ programmer

I am primarily a C ++ encoder and have not touched C # after a few years (so forgive me if I ask a question, which may be brain fart in my part).

Background information: I am writing a utility for organizing files (just for fun and for cleaning duplicates on my computer). I was able to run MD5 checksums for files and files and find duplicate files in different subdirectories, sometimes with the same file name, and sometimes not (always the same file type). I initially did this using only the lines of the file path and Winform, Arraylist, Arrays objects in the code for "MainFrom", as a proof of concept. The code is really ugly, and I'm already confused looking at it, so it is definitely not supported.

So I decided that a more elegant and reasonable design would be stored as an object that would have simple things like filepath, filename, MD5, fileType, etc. (yes, I know about FILE). Then I realized that it makes sense if it had references to other instances of objects that were "duplicates", "similar." I am looking to create an array of pointers that is part of an object that points to duplicate objects. Thus, at runtime, I could go through all the duplicates, find out their properties, etc.

I planned to later inherit this class for certain types of files, such as mp3 or jpg files, where I can compare content (for example: I could determine which pictures can simply be changed by versions of each other and point them to each other). But C # has no pointers. I looked at the delegates, but then again, this is not what I want.

My dilemma: C # doesn't have a pointer in managed code (I don't want to use unmanaged sections unless I need it).

I also thought about creating something like an arraylist and passing in "objects" at runtime. But does this not create duplicates? Is this not a link to a new object?

I would really like the advice from those who made the transition from C ++ to C # , as I move beyond this. Please feel free to let me know if I totally disagree with the design here. (I assume that since they are both object oriented, such a design will work in both worlds).

I would really like links to other sources that may help (since I'm not the first C ++ code trying to encode in C #). Thank you in advance!

+4
source share
7 answers

My dilemma: C # doesn't have a pointer in managed code

But it uses references for objects everywhere (including arrays).

var a = new StringBuilder(); var b = a; // a and b now refer to the same single StringBuilder instance a.Append("!"); // equivalent to a->Append("!"); in C++ 

So, what functionality do you actually lack, what do you think (only) pointers can solve?

creating something like an arraylist and passing in "objects" at runtime. But does this not create duplicates? Is this not a link to a new object?

No, ArrayList can only store references to objects, so copying or cloning is usually not required. This only happens for value types (int, double). But be sure to use List<MyClass> instead, for more functionality and fewer types.

In short, read β€œLink Types,” β€œValue Types,” and links before proceeding.

+6
source

C # link types are (almost) always passed as pointers. If I do this:

 var a = new Object(); var list1 = new List<Object>(); var list2 = new List<Object>(); list1.Add(a); list2.Add(a); 

I do not duplicate a. I just put a link to it on both lists. Hope this helps.

+4
source
  A a1; A a2 = a1; 

In C ++, the above code will make a1 and a2 two separate objects that are copies of each other. In C #, if A is declared as a class a1, and a2 will refer to the same object so that you do not indicate on your own what you are trying to achieve

+2
source

Instances of classes in C # are references and, therefore, are similar to pointers. In C #, when you write a = b , where a and b are instances of classes, the only thing that is copied is an object reference. Now you have two links to the same object.

When I read your question, C # links will serve your purpose.

0
source

Arraylist will do your job well. Objects passed to arraylist are just references (similar to pointers). They are not recreated, so there will be no duplicates.

0
source

You do not need to deal with pointers since C # passes references for object types.

And you should take a look at the HashSet / generic Lists that C # provides as collections.

0
source

Another way to solve a pointer problem in C #. WeakReference . http://msdn.microsoft.com/en-us/library/ms404247.aspx

C # "shared pointer" for alternative memory management?

Example:

 // Create the object Book book = new Book("My first book", "Me"); // Set weak reference WeakReference wr = new WeakReference(book); // Remove any reference to the book by making it null book = null; if (wr.IsAlive) { Console.WriteLine("Book is alive");\ Book book2 = wr.Target as Book; Console.WriteLine(book2.Title); book2 = null; } else Console.WriteLine("Book is dead"); // Lets see what happens after GC GC.Collect(); // Should not be alive if (wr.IsAlive) Console.WriteLine("Book is alive"); else Console.WriteLine("Book is dead"); 

The exit should be

 Book is alive My first book Book is dead 

http://www.switchonthecode.com/tutorials/csharp-tutorial-weak-references

0
source

Source: https://habr.com/ru/post/1392084/


All Articles