Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Questions

C# Questions

Scheduled Pinned Locked Moved C#
csharpquestionc++dotnetgraphics
21 Posts 10 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    shiftwik wrote:

    you need the functionality of memory allocation and calling the constructor

    It's an OO language. That means one would be creating objects. 2.

    shiftwik wrote:

    why does C# require the "return" keyword with a "get" function

    ..because that's how functions MUST return their value. The getter is merely function, and the last statement of such is always a return-statement. 3.

    shiftwik wrote:

    When CLR executes your code is the .net "engine" accessing kernel

    The WinAPI. So, yes. 4.

    shiftwik wrote:

    Does C# have an equivalent to the Doc/View architecture for windows apps?

    I'm too lazy to Google for Doc/View :)

    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

    S Offline
    S Offline
    shiftwik
    wrote on last edited by
    #3

    thanks Eddy, great reply!!! you must be a college professor Can anybody here help me with some intelligent in depth answers??

    1 Reply Last reply
    0
    • S shiftwik

      1. Why does C# require the "new" keyword? why bother?? it's not like C++ where you need the functionality of memory allocation and calling the constructor 2. why does C# require the "return" keyword with a "get" function. What else would you use a get function for? besides returning data?? seems like an extra step where "get" is built in to the language 3. When CLR executes your code is the .net "engine" accessing kernel, user and or gdi.dll ? or does it have some other method of communicating with windows os kernel functions? 4. Does C# have an equivalent to the Doc/View architecture for windows apps? thanks

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #4
      1. For exactly the same reason as in C++. 2) If you don't like it, try VB.

      You'll never get very far if all you do is follow instructions.

      1 Reply Last reply
      0
      • S shiftwik

        1. Why does C# require the "new" keyword? why bother?? it's not like C++ where you need the functionality of memory allocation and calling the constructor 2. why does C# require the "return" keyword with a "get" function. What else would you use a get function for? besides returning data?? seems like an extra step where "get" is built in to the language 3. When CLR executes your code is the .net "engine" accessing kernel, user and or gdi.dll ? or does it have some other method of communicating with windows os kernel functions? 4. Does C# have an equivalent to the Doc/View architecture for windows apps? thanks

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #5

        1. Because the new keyword tells the application to instantiate an object at that point. If we have several different constructors on an object, new is a great way to tell the application that you expect a certain constructor to be invoked at that point. Merely referencing the object wouldn't be a great place to instantiate it because you could instantiate it long before you need it - and this would play hell doing things like building class factories. 2. Get is synctatic sugar that hides the fact that you are actually calling a special method with the prefix get_, so your code has to follow the standards of other methods. Another point to consider is, how would you indicate what you are returning? It's quite common to have some form of lazy initialisation or return choice in the getter, so you have to have some functionality in there to indicate what's going to be returned. Of course, if you don't actually need to do anything, just create an automatic property and you don't have to write a return statement (although one is implicitly created behind the scenes). 3. Yes. The .NET runtime and .NET applications are still PE Format applications. 4. That's just a pattern. While there's no default implementation of it in the framework, it's incredibly trivial to create although, as anyone who's worked with DocView will attest, the DocView pattern is severely limited in what it can do. There are a number of superior patterns available to use, such as MVC.

        Richard Andrew x64R S 2 Replies Last reply
        0
        • S shiftwik

          1. Why does C# require the "new" keyword? why bother?? it's not like C++ where you need the functionality of memory allocation and calling the constructor 2. why does C# require the "return" keyword with a "get" function. What else would you use a get function for? besides returning data?? seems like an extra step where "get" is built in to the language 3. When CLR executes your code is the .net "engine" accessing kernel, user and or gdi.dll ? or does it have some other method of communicating with windows os kernel functions? 4. Does C# have an equivalent to the Doc/View architecture for windows apps? thanks

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #6
          1. Because if it didn't it wouldn't know when you wanted to create a new instance:

          MyClass[] data = new MyClass[10];
          MyClass instance;
          for (int i = 0; i < 10; i++)
          {
          data = instance;
          }

          How many different instances of MyClass should that produce? Zero? One? Ten? Or Eleven?

          MyClass[] data = new MyClass[10];
          MyClass instance;
          for (int i = 0; i < 10; i++)
          {
          instance = new MyClass();
          data = instance;
          }

          Makes that abundantly clear. 2) See previous answers, particularly Pete's 3) Yes. Again, see Pete's answer. 4) No, thank goodness. D/V was good in it's time, but thankfully things have moved on a lot since those primitive days.

          Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          S 1 Reply Last reply
          0
          • P Pete OHanlon

            1. Because the new keyword tells the application to instantiate an object at that point. If we have several different constructors on an object, new is a great way to tell the application that you expect a certain constructor to be invoked at that point. Merely referencing the object wouldn't be a great place to instantiate it because you could instantiate it long before you need it - and this would play hell doing things like building class factories. 2. Get is synctatic sugar that hides the fact that you are actually calling a special method with the prefix get_, so your code has to follow the standards of other methods. Another point to consider is, how would you indicate what you are returning? It's quite common to have some form of lazy initialisation or return choice in the getter, so you have to have some functionality in there to indicate what's going to be returned. Of course, if you don't actually need to do anything, just create an automatic property and you don't have to write a return statement (although one is implicitly created behind the scenes). 3. Yes. The .NET runtime and .NET applications are still PE Format applications. 4. That's just a pattern. While there's no default implementation of it in the framework, it's incredibly trivial to create although, as anyone who's worked with DocView will attest, the DocView pattern is severely limited in what it can do. There are a number of superior patterns available to use, such as MVC.

            Richard Andrew x64R Offline
            Richard Andrew x64R Offline
            Richard Andrew x64
            wrote on last edited by
            #7

            Pete O'Hanlon wrote:

            There are a number of superior patterns available to use, such as MVC.

            Would that be the Model View Controller pattern you're referring to there? I didn't realize that was applicable to anything but web applications, but I'll be happy to try a different pattern with C++.

            The difficult we do right away... ...the impossible takes slightly longer.

            P 1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              Pete O'Hanlon wrote:

              There are a number of superior patterns available to use, such as MVC.

              Would that be the Model View Controller pattern you're referring to there? I didn't realize that was applicable to anything but web applications, but I'll be happy to try a different pattern with C++.

              The difficult we do right away... ...the impossible takes slightly longer.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #8

              I was using MVC back in the late 90s with MFC - yes, for desktop apps. Ah, those were some fun days.

              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff
                1. Because if it didn't it wouldn't know when you wanted to create a new instance:

                MyClass[] data = new MyClass[10];
                MyClass instance;
                for (int i = 0; i < 10; i++)
                {
                data = instance;
                }

                How many different instances of MyClass should that produce? Zero? One? Ten? Or Eleven?

                MyClass[] data = new MyClass[10];
                MyClass instance;
                for (int i = 0; i < 10; i++)
                {
                instance = new MyClass();
                data = instance;
                }

                Makes that abundantly clear. 2) See previous answers, particularly Pete's 3) Yes. Again, see Pete's answer. 4) No, thank goodness. D/V was good in it's time, but thankfully things have moved on a lot since those primitive days.

                Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                S Offline
                S Offline
                shiftwik
                wrote on last edited by
                #9

                thanks ... I guess my question on "new" is why isn't it just automatic? with C# it call delete for you. So why not new also? would you ever not use it?

                OriginalGriffO 2 Replies Last reply
                0
                • P Pete OHanlon

                  1. Because the new keyword tells the application to instantiate an object at that point. If we have several different constructors on an object, new is a great way to tell the application that you expect a certain constructor to be invoked at that point. Merely referencing the object wouldn't be a great place to instantiate it because you could instantiate it long before you need it - and this would play hell doing things like building class factories. 2. Get is synctatic sugar that hides the fact that you are actually calling a special method with the prefix get_, so your code has to follow the standards of other methods. Another point to consider is, how would you indicate what you are returning? It's quite common to have some form of lazy initialisation or return choice in the getter, so you have to have some functionality in there to indicate what's going to be returned. Of course, if you don't actually need to do anything, just create an automatic property and you don't have to write a return statement (although one is implicitly created behind the scenes). 3. Yes. The .NET runtime and .NET applications are still PE Format applications. 4. That's just a pattern. While there's no default implementation of it in the framework, it's incredibly trivial to create although, as anyone who's worked with DocView will attest, the DocView pattern is severely limited in what it can do. There are a number of superior patterns available to use, such as MVC.

                  S Offline
                  S Offline
                  shiftwik
                  wrote on last edited by
                  #10

                  thanks,

                  1 Reply Last reply
                  0
                  • S shiftwik

                    thanks ... I guess my question on "new" is why isn't it just automatic? with C# it call delete for you. So why not new also? would you ever not use it?

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #11

                    Simply because C# doesn't try to "guess" what you wanted. Think about it: I don't want the system creating a new instance every time I try to use an existing one, because it means a trip to the DB and back to create an item that I may not use again - and the "real" DB item then doesn't get updated. I want the system to create an instance only when I specifically tell it to. If the system created them for me, then

                    MyClass[] data = MyClass[10];

                    Would create an array of references to MyClass instances and the instances to fill it with. I may not want that: if MyClass always contains an enormous Bitmap (for example) that is a huge amount of time and memory being wasted, because I'm about to fill the array with the top ten instances from the DB when I call the method below it:

                    MyClass[] data = MyClass[10];
                    DAL.GetImageData(data, 10, "SEARCH CONDITION");

                    But the system can't know that because it doesn't have any idea what the method does - it's in a separate DLL! The new keyword allow you to specify exactly when you want an instance created (and reminds you that this could take some time and / or resources when it does it)

                    Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    E 1 Reply Last reply
                    0
                    • S shiftwik

                      thanks ... I guess my question on "new" is why isn't it just automatic? with C# it call delete for you. So why not new also? would you ever not use it?

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #12

                      BTW:

                      shiftwik wrote:

                      with C# it call delete for you

                      No, it doesn't - it calls Dispose for you, but not at a time of your chosing, unless you specifically add a using block: objects are only ever deleted when the Garbage Collector gets around to it, which may never happen! :laugh:

                      Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      Richard DeemingR 1 Reply Last reply
                      0
                      • S shiftwik

                        1. Why does C# require the "new" keyword? why bother?? it's not like C++ where you need the functionality of memory allocation and calling the constructor 2. why does C# require the "return" keyword with a "get" function. What else would you use a get function for? besides returning data?? seems like an extra step where "get" is built in to the language 3. When CLR executes your code is the .net "engine" accessing kernel, user and or gdi.dll ? or does it have some other method of communicating with windows os kernel functions? 4. Does C# have an equivalent to the Doc/View architecture for windows apps? thanks

                        V Offline
                        V Offline
                        V 0
                        wrote on last edited by
                        #13

                        2. You could do something like this:

                        public int LimitedInt{
                        get{
                        if(mylimitedint < 0){
                        limitedint = 0;
                        }
                        if(limitedint > 42){
                        limitedint = 42;
                        }
                        return limitedint;
                        }
                        }

                        3. Yes everything that has something to do with the OS will pass through the kernel, but it passes through the .NET framework first. IOW the framework adds functionality to your code. Among many things Garbage Collection, Exception handling etc ...

                        V.
                        (MQOTD rules and previous solutions)

                        1 Reply Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          BTW:

                          shiftwik wrote:

                          with C# it call delete for you

                          No, it doesn't - it calls Dispose for you, but not at a time of your chosing, unless you specifically add a using block: objects are only ever deleted when the Garbage Collector gets around to it, which may never happen! :laugh:

                          Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                          Richard DeemingR Online
                          Richard DeemingR Online
                          Richard Deeming
                          wrote on last edited by
                          #14

                          Dispose will only ever be called at a time of your choosing - either from the end of a using block, or when you explicitly call it. It's the finalizer - ~ClassName(){ ... } - that gets called by the GC at some random time in the future.


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                          OriginalGriffO 1 Reply Last reply
                          0
                          • Richard DeemingR Richard Deeming

                            Dispose will only ever be called at a time of your choosing - either from the end of a using block, or when you explicitly call it. It's the finalizer - ~ClassName(){ ... } - that gets called by the GC at some random time in the future.


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                            OriginalGriffO Offline
                            OriginalGriffO Offline
                            OriginalGriff
                            wrote on last edited by
                            #15

                            :doh: Yes, you are right - and the finalizer should call Dispose(false)... I plead stupidity...and a lack of caffeine.

                            Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                            N 1 Reply Last reply
                            0
                            • OriginalGriffO OriginalGriff

                              :doh: Yes, you are right - and the finalizer should call Dispose(false)... I plead stupidity...and a lack of caffeine.

                              Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                              N Offline
                              N Offline
                              Nicholas Marty
                              wrote on last edited by
                              #16

                              Should call Dispose if you are implementing the IDisposable Interface :-\ You usually only use it to ensure that unmanaged resources are released, to prevent memory leaks. Either immediatly when Dispose gets called or when the GC calls the Finalize Method (which you have to override in that case), in case it didn't get called by the code which held the reference to it. You might also use it to free resources before the GC kicks in (by setting large fields like lists etc. to null and thus making it easier for the GC to collect those resources). ;P

                              Richard DeemingR 1 Reply Last reply
                              0
                              • N Nicholas Marty

                                Should call Dispose if you are implementing the IDisposable Interface :-\ You usually only use it to ensure that unmanaged resources are released, to prevent memory leaks. Either immediatly when Dispose gets called or when the GC calls the Finalize Method (which you have to override in that case), in case it didn't get called by the code which held the reference to it. You might also use it to free resources before the GC kicks in (by setting large fields like lists etc. to null and thus making it easier for the GC to collect those resources). ;P

                                Richard DeemingR Online
                                Richard DeemingR Online
                                Richard Deeming
                                wrote on last edited by
                                #17

                                Nicholas Marty wrote:

                                Should call Dispose if you are implementing the IDisposable Interface :-\

                                If your class has a finalizer and doesn't implement IDisposable, that's almost certainly a bug. I can't think of any reason why your class would say, "I want this resource to possibly be cleaned up at some unknown point in the future, but I don't want the calling code to be able to clean it up immediately".


                                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                N 1 Reply Last reply
                                0
                                • Richard DeemingR Richard Deeming

                                  Nicholas Marty wrote:

                                  Should call Dispose if you are implementing the IDisposable Interface :-\

                                  If your class has a finalizer and doesn't implement IDisposable, that's almost certainly a bug. I can't think of any reason why your class would say, "I want this resource to possibly be cleaned up at some unknown point in the future, but I don't want the calling code to be able to clean it up immediately".


                                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                  N Offline
                                  N Offline
                                  Nicholas Marty
                                  wrote on last edited by
                                  #18

                                  Every Type has a finalizer as every object inherits from System.Object at some point. You may or may not override it. But it still has a finalizer ;)

                                  Richard DeemingR 1 Reply Last reply
                                  0
                                  • N Nicholas Marty

                                    Every Type has a finalizer as every object inherits from System.Object at some point. You may or may not override it. But it still has a finalizer ;)

                                    Richard DeemingR Online
                                    Richard DeemingR Online
                                    Richard Deeming
                                    wrote on last edited by
                                    #19

                                    Object.Finalize Method[^]

                                    The Object class provides no implementation for the Finalize method, and the garbage collector does not mark types derived from Object for finalization unless they override the Finalize method.

                                    So whilst it's technically true that every object has a finalizer, only objects which override the finalizer will be marked for finalization. :)


                                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                    N 1 Reply Last reply
                                    0
                                    • Richard DeemingR Richard Deeming

                                      Object.Finalize Method[^]

                                      The Object class provides no implementation for the Finalize method, and the garbage collector does not mark types derived from Object for finalization unless they override the Finalize method.

                                      So whilst it's technically true that every object has a finalizer, only objects which override the finalizer will be marked for finalization. :)


                                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                      N Offline
                                      N Offline
                                      Nicholas Marty
                                      wrote on last edited by
                                      #20

                                      Yeah. And you should suppress the finalization of the object if you disposed the object already :) And yeah, if you override the finalizer you should probably also use the IDisposable pattern.

                                      1 Reply Last reply
                                      0
                                      • OriginalGriffO OriginalGriff

                                        Simply because C# doesn't try to "guess" what you wanted. Think about it: I don't want the system creating a new instance every time I try to use an existing one, because it means a trip to the DB and back to create an item that I may not use again - and the "real" DB item then doesn't get updated. I want the system to create an instance only when I specifically tell it to. If the system created them for me, then

                                        MyClass[] data = MyClass[10];

                                        Would create an array of references to MyClass instances and the instances to fill it with. I may not want that: if MyClass always contains an enormous Bitmap (for example) that is a huge amount of time and memory being wasted, because I'm about to fill the array with the top ten instances from the DB when I call the method below it:

                                        MyClass[] data = MyClass[10];
                                        DAL.GetImageData(data, 10, "SEARCH CONDITION");

                                        But the system can't know that because it doesn't have any idea what the method does - it's in a separate DLL! The new keyword allow you to specify exactly when you want an instance created (and reminds you that this could take some time and / or resources when it does it)

                                        Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                                        E Offline
                                        E Offline
                                        Erik Westermann
                                        wrote on last edited by
                                        #21

                                        To add to your excellent reply: related is lazy initialization lazy initialization[^] The .net framework convenieitly offers direct support: the Lazy Class[^]

                                        Erik Westermann

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups