C# ?

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

hans007

Lifer
Feb 1, 2000
20,212
18
81
i'm primarily a c++ programmer. though i've dabbled with c# and occassionally have to program in java.

first language at my college just happened to be c++ and the 3rd class was all in ASM. I had learned c++ in high school though, and I think generally you can learn the "easier" languages much more easily starting form hard to easy, than starting from easy to hard. if a college is going to teach computer science they should do it with the real basics. i suppose wheni was in college it was sort of a weeding out. ..... if you couldn't figure out how to use interrupts and asm well, you werent gonna make it.

C# / java does seem like where things are going. or well c# seems like where microsoft wants to steer the world. I honestly cant see what is so obsolete about c++ , but I suppose that just might be that I'm comfortable with it.
 

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
Dispose is incredibly useful and a Microsoft best practice for pretty much all of their product APIs. Enjoy crappy performance in SharePoint if you don't do site.RootWeb.Dispose() and site.Dispose().

Josh, thats because they have unmanaged items to release. Thats why the interface was designed. If you have unmanaged objects you SHOULD be using Dispose. The poster suggested that using Dispose means the garbage collector has less work to do, which is completely false.

 

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
Maybe you should look up on how the CLR manages garbage collection and what "Dispose" and "unmanaged" actually mean.

Actually, you do. (I was on the external design review committee). You have some of the basics right but are missing the point on how it actually works.

You're correct when you say that Dispose is useful when dealing with unmanaged resources. But you're wrong when you claim that developers aren't minimizing garbage collections by disposing objects that are actually disposable. Many folks who never had any formal training on how the CLR works carry this misconception. I used to, too

The same amount of garbage collection is going to be happening in either case. The purpose of dispose is to deterministically control when the unmanged item will be released. The LAST thing you want is to be holding onto a sql connection until the CRL gets around to garbage collecting the object that was holding it since you can't (generally, or more specifically shouldn't be controlling) when garbage collection occurs.

which tells the CLR not to GC that object

That is completely wrong. GC.SuppressFinalize tells the garbage collector to not call the finalizer when it DOES garbage collect the object. The garbage collector still needs to release the memory used by the object, it just knows not to call finalize first (since you've told it you've release all other object references and it doesn't need to bother with the call). The code/cpu time is the same. Either your going to finalize in your dispose or the garbage collector is going to call finalize when it reclaims the memory. But where you got the notion that it tells the CLR not to garbage collect the object I have no idea.

Internally SupressFinalize is a link list of object that have already been finalized, the GC knows for those objects it can free their memory or rebuild contigous memory regions depending on its needs. The address space the object occupied by ever means still needs to be garbage collected.

it, you're actually freeing resources. While by calling GC.SuppressFinalize you're minimizing all the CPU cycles that the CLR would have spent in cleaning up that object (note that this is not even possible, because the CLR can't clean up unmanaged resources... hence the IDisposable pattern).

Again its a wash since you spent those cycles in dispose. The only difference from the system view is that you now control when unmanaged items are released (which is a good thing).

 

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
Memory management is handled by the CLR, but it is still very important. I personally, manually dispose (using blocks) all my objects in my C# code to minimize garbage collections at the CLR level...

Btw, what you say above is right, but for the wrong reasons. You should be using 'using blocks' and you should be ensuring that objects with dispose methods have dispose (aka close, etc) called as soon as possible withing your code path.

Your error is in believing this is to minimize garbage collection, it is not. It is actually to ensure those unmanaged resources get released to the system as soon as possible as they are often a limited resource (e.g. file handle, sql handle, etc).

 

Gunslinger08

Lifer
Nov 18, 2001
13,234
2
81
Originally posted by: bsobel
Dispose is incredibly useful and a Microsoft best practice for pretty much all of their product APIs. Enjoy crappy performance in SharePoint if you don't do site.RootWeb.Dispose() and site.Dispose().

Josh, thats because they have unmanaged items to release. Thats why the interface was designed. If you have unmanaged objects you SHOULD be using Dispose. The poster suggested that using Dispose means the garbage collector has less work to do, which is completely false.

Yeah, I know. MS needs to re-write their code base to get rid of the unmanaged objects. Most of it is left over from versions that are 5-10+ years old.
 

imported_Dhaval00

Senior member
Jul 23, 2004
573
0
0
Memory management is handled by the CLR, but it is still very important. I personally, manually dispose (using blocks) all my objects in my C# code to minimize garbage collections at the CLR level...
Btw, what you say above is right, but for the wrong reasons. You should be using 'using blocks' and you should be ensuring that objects with dispose methods have dispose (aka close, etc) called as soon as possible withing your code path.

Every MS API (not almost every) that I know of, calls Dispose() from within Close(). In fact MS asks you to call Dispose from within Close. As long as you keep track of this, calling Dispose not only closes/frees the resource, but puts less burden on the GC. Here is an example: http://msdn2.microsoft.com/en-...lconnection.close.aspx. Look for the line that states "Close() is equivalent to Dispose()." By calling Dispose directly, you're skipping calling Close in most cases . Of course this is not a major thing, but it does *minutely* speed up things in an enterprise application. If third-party API's don't follow this trend, it is not MS's fault.

That is completely wrong. GC.SuppressFinalize tells the garbage collector to not call the finalizer when it DOES garbage collect the object. The garbage collector still needs to release the memory used by the object, it just knows not to call finalize first (since you've told it you've release all other object references and it doesn't need to bother with the call). The code/cpu time is the same. Either your going to finalize in your dispose or the garbage collector is going to call finalize when it reclaims the memory. But where you got the notion that it tells the CLR not to garbage collect the object I have no idea.

I did state it incorrectly. I meant it tells GC not to finalize the object. Again, my point stays: The managed environment doesn't know anything about how to clean up unmanaged resources. *You* by calling Dispose, are freeing memory and resources. Also your statement "Either your going to finalize in your dispose or the garbage collector is going to call finalize when it reclaims the memory" is erroneous. You actually call your Dispose method from within the Finalizer (not the other way round as you stated). And you do this, because the Framework doesn't guarantee if Dispose will be called... but it does call the Finalizers. Look here: http://msdn.microsoft.com/msdn...es/07/07/CLRInsideOut/
Here is an excerpt from the article: "Specifically, the garbage collector will never call your Dispose method for you. The only cleanup method that the garbage collector will call is the object?s finalizer; if code is not written to explicitly call the Dispose method, then the method will never be called."

Internally SupressFinalize is a link list of object that have already been finalized, the GC knows for those objects it can free their memory or rebuild contigous memory regions depending on its needs. The address space the object occupied by ever means still needs to be garbage collected.
Again its a wash since you spent those cycles in dispose. The only difference from the system view is that you now control when unmanaged items are released (which is a good thing).

I am thinking this was a typo, but I dont know what you mean when you say "SuppressFinalize is a link list of object that have already been finalized?" SuppressFinalize is a method and takes in a single object parameter that needs to be removed from the set of objects maintained by the GC that require finalization. Even Jeffery Richter's CLR via C# asserts the same thing - Calling SuppressFinalize() (assuming you implement both a Finalizer and 2 Dispose() methods) puts less burden on the GC. I should have stated in my previous post that as far as the process is concerned, it puts less burden on the memory... somehow I digressed to discussing CPU cycles. *But* even in terms of CPU cycles, you're helping the GC by not forcing it to resurrect the full object graph for finalization. Look here, http://msdn.microsoft.com/msdnmag/issues/1100/GCI/, under the topic "Forcing an object to Clean Up." Here is an excerpt from the article: "When you call SuppressFinalize, it turns on a bit flag associated with the object. When this flag is on, the runtime knows not to move this object's pointer to the freachable queue, preventing the object's Finalize method from being called." Also, refer to the example that Jefferey provides in order to show this. By all means, this is a dated article, and here is a more recent explaination: http://msdn2.microsoft.com/en-...m.object.finalize.aspx


I argued your initial post because it simply asserted (too naively) that you as a developer don't need to worry about memory management in the managed world. This sounded too easy, and also becomes a pick-up line for .NET-bashers... to them it is a very simple life in the .NET World

I'll take your word that you were on the external design review committee. I am just curious as to which one: .NET 1.0, .NET 1.1, .NET 2.0, or .NET 3.5? Almost every iteration of the Framework undergoes immense changes and it is hard for me to acknowledge someone's assertions that may date back to .NET 1.0 days (no offense). Same can be applied to my assertions throughout this post, but I am always learning something new, and I'll openly accept my mistakes. I may have typos (I get excited by .NET discussions), but I'll try and back my assertions via realistic articles/books in most cases.
 

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
I argued your initial post because it simply asserted (too naively) that you as a developer don't need to worry about memory management in the managed world.

I never suggested such a thing, I was making a joke. Re-read my comment in context.

As for your other comments, I'll have to answer when I get back on Labor Day (we are in the middle of packing a leaving). Short version is still Dispose allows you to control when unmanaged resources are free'd, it doesn't take any load of the GC. But I'll answer more fully when I get back.

Cheers,
Bill

 

imported_Dhaval00

Senior member
Jul 23, 2004
573
0
0
I'll be looking forward to it. I would love to see some sources... never hurts to better understand the inner workings (assuming I am completely/partially wrong).

Have a nice vacation, btw.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: Dhaval00
Originally posted by: bsobel
Memory management is very important, but it isn't an easy concept to master and should be introduced after other concepts.

So what are we going to teach once garbage collection is formally added to the c++ standard (It's in work, one of my co-workers is one of the primary guys behind it).

The C++/CLI built on top of the CLR 2.0 already does this...

I think they should continue teaching native C and/or C++ in colleges, but at the same time keep this topic limited. There are more emphasis on higher level languages that are more productive (Java, C#, etc.) in the real world. I did feel cut-off 2 years back while trying to land a new job when I graduated with C++ skills on my one-page resume. Having seen how most companies do their businesses, it is imperative that the push is for SOA with minimal depedency on lower-level languages like native C++.

Memory management is handled by the CLR, but it is still very important. I personally, manually dispose (using blocks) all my objects in my C# code to minimize garbage collections at the CLR level... I know quite a few developers who went to private universities and don't dispose their objects - they take memory management for granted. Simply put, they either were trained primarily in C# without formal knowledge of the internals of the CLR or had no classes in native C/C++.

Ultimately, I have dealt with quite a few companies and senior colleagues who think this is very important... specifically, during interviews, we'll ask questions about CLR's IDisposable, finalization, and object management. So whoever thinks memory management is a sparse topic that needs to be forgotten needs to wake up *now*.
Hmm, I had been arguing that most programmers can forget about memory management but after reading this and the rest of the discussion it's pretty obvious I'm wrong. I'd argue that memory management is still a less important skill and that you're actually talking about the management of other resources but clearly the principles are the same and everyone still needs to know them.

It just struck me as interesting how java and c# programmers frequently try to release everything in the same place they allocate it, using using or try/catch blocks which is a pattern I haven't noticed in my limited c programming experience. In c it's always felt more like *alloc() in one part of the program and free() somewhere on the other side and try damn hard to keep track of it all in between to prevent memory leaks. Does that dichotomy strike anyone else or am I imagining it?
 

Schadenfroh

Elite Member
Mar 8, 2003
38,416
4
0
C# replaced SmallTalk and other languages in several of the higher level classes at my university this year, although the first two years are still primarily C++ (the first language taught to CS majors at my university), assembly (the 2nd language taught to CS majors at my university, we switched from Motorola to Intel chips this year), hardware (workings of digital computer systems + study of circuits / processor architecture, feels like an electrical engineering class), linear programming and loads upon loads of straight calculus classes topped off with some nice calculus based probability classes.

A huge number of changes in the curriculum actually, came about after the retirement of several professors when our president stepped down.
 

hans007

Lifer
Feb 1, 2000
20,212
18
81
i think thats probably the best way to learn. learning C++ and asm first as well as the hardware stuff is probably more difficult, but in the end it will "stick" better and maybe give you a better understanding. besides if you cant "get" that you probably are going to have problems later anyway
 

erwos

Diamond Member
Apr 7, 2005
4,778
0
76
Originally posted by: kamper
Hmm, I had been arguing that most programmers can forget about memory management but after reading this and the rest of the discussion it's pretty obvious I'm wrong. I'd argue that memory management is still a less important skill and that you're actually talking about the management of other resources but clearly the principles are the same and everyone still needs to know them.
At the very minimum, your machine has a finite amount of memory. When you run out of it because you've used it all, things go to hell, whether or not you've got GC. For some things, this won't happen. For others (sophisticated data mining on huge data sets), you can run into some real issues if you're not thinking about what you're doing. I actually worry more about RAM on some of my Perl scripts for data mining than I do with my C++ code for other stuff.

The discussion about Dispose was interesting, if only because I'm generally not using a lot of unmanaged resources in my code. Thanks!
 

jman19

Lifer
Nov 3, 2000
11,224
659
126
I don't think anyone disagrees that you should have an understanding of how the machine you are programming for works, virtual or real, but manual memory management concepts are difficult to master and are probably just frustrating for newbies.
 

aCynic2

Senior member
Apr 28, 2007
710
0
0
Originally posted by: brandonb
The languages I see out there in the field being used regularly:

C#
VB.Net
Java

Learn those if you want a job.

I don't want a job. I want to accomplish much more than that.
 

AlfB

Member
May 20, 2003
26
0
0
I think the key that no one seems to mention here is that the answer to this question has a lot to do with which direction you may or may not want to go with your career. Granted its difficult to decide something that big too early but you need to start somewhere. If you are going toward a lower level of programming such as drivers, DLLs or hardware control, then C is the place to start. C will also be a good basis even if you move to a higher level later and need C#. C# is mainly useful in a MS environment which not everything programmed will use. There are quite a few programming jobs out there that have nothing to do with an MS environment. Most of them will have some use for C though.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |