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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. The Lounge
  3. Quick C# quiz

Quick C# quiz

Scheduled Pinned Locked Moved The Lounge
csharphtmlquestion
45 Posts 15 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.
  • honey the codewitchH honey the codewitch

    I admire your self confidence. When even the compiler disagrees with you, you know you've won.

    When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

    Sander RosselS Offline
    Sander RosselS Offline
    Sander Rossel
    wrote on last edited by
    #22

    I'm so confident I file a bug report when the compiler refuses to compile MY code :laugh: Inspired by Chuck Norris, who doesn’t get compiler errors, the language changes itself to accommodate. Seriously though, style is (somewhat) important and I think your question is a great example of how having consistent styling can make reading code so much more easy :) You're probably wondering because you somehow need to parse/lex/tokenize it, in which case the other person was right, you write for compilers (maybe that's what he meant) :laugh:

    Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

    honey the codewitchH 1 Reply Last reply
    0
    • Sander RosselS Sander Rossel

      I'm so confident I file a bug report when the compiler refuses to compile MY code :laugh: Inspired by Chuck Norris, who doesn’t get compiler errors, the language changes itself to accommodate. Seriously though, style is (somewhat) important and I think your question is a great example of how having consistent styling can make reading code so much more easy :) You're probably wondering because you somehow need to parse/lex/tokenize it, in which case the other person was right, you write for compilers (maybe that's what he meant) :laugh:

      Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

      honey the codewitchH Offline
      honey the codewitchH Offline
      honey the codewitch
      wrote on last edited by
      #23

      That's because Chuck Norris doesn't code in C++, C++ codes in Chuck Norris. Style is important and if I had my way I'd make my little parser enforce my own naming conventions as it would be a bit easier on me. But I'm not here for that. I'm here for you and I each being able to use this thing how we want, even while fighting with each other over how to use it properly. :laugh: I used to use hungarian notation. Now I've evolved to not bother with that, and instead focus on other things because of tools like intellisense. I always try to code in a way that works well with the tools I use and that looks at home with the tools I use. That's why I adopt different naming styles and even coding guidelines depending on context. My C++ library code uses different conventions than my other C++ code which uses different conventions than my C# code which uses different conventions than my java code, etc. My XML naming styles are different depending on context, and json naming style tends to be camelCase. Context is everything.

      When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

      Sander RosselS 1 Reply Last reply
      0
      • honey the codewitchH honey the codewitch

        That's because Chuck Norris doesn't code in C++, C++ codes in Chuck Norris. Style is important and if I had my way I'd make my little parser enforce my own naming conventions as it would be a bit easier on me. But I'm not here for that. I'm here for you and I each being able to use this thing how we want, even while fighting with each other over how to use it properly. :laugh: I used to use hungarian notation. Now I've evolved to not bother with that, and instead focus on other things because of tools like intellisense. I always try to code in a way that works well with the tools I use and that looks at home with the tools I use. That's why I adopt different naming styles and even coding guidelines depending on context. My C++ library code uses different conventions than my other C++ code which uses different conventions than my C# code which uses different conventions than my java code, etc. My XML naming styles are different depending on context, and json naming style tends to be camelCase. Context is everything.

        When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

        Sander RosselS Offline
        Sander RosselS Offline
        Sander Rossel
        wrote on last edited by
        #24

        honey the codewitch wrote:

        That's why I adopt different naming styles and even coding guidelines depending on context. My C++ library code uses different conventions than my other C++ code which uses different conventions

        I could agree there if I (and my team) always used the same tools. Which I (and formerly, we) do, so my style is adjusted to VS2012+. If I sometimes used VS2019 and other times VS Code and they somehow had different styles then I'd still stick with one style.

        honey the codewitch wrote:

        C# code which uses different conventions than my java code, etc. My XML naming styles are different depending on context, and json naming style tends to be camelCase

        That's just common sense. Or did you think me some barbarian who uses PascalCasing in JavaScript as well!? :wtf: I've seen JavaScript written like it was C# and it really hurts the senses :sigh:

        Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

        honey the codewitchH 1 Reply Last reply
        0
        • Sander RosselS Sander Rossel

          honey the codewitch wrote:

          That's why I adopt different naming styles and even coding guidelines depending on context. My C++ library code uses different conventions than my other C++ code which uses different conventions

          I could agree there if I (and my team) always used the same tools. Which I (and formerly, we) do, so my style is adjusted to VS2012+. If I sometimes used VS2019 and other times VS Code and they somehow had different styles then I'd still stick with one style.

          honey the codewitch wrote:

          C# code which uses different conventions than my java code, etc. My XML naming styles are different depending on context, and json naming style tends to be camelCase

          That's just common sense. Or did you think me some barbarian who uses PascalCasing in JavaScript as well!? :wtf: I've seen JavaScript written like it was C# and it really hurts the senses :sigh:

          Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

          honey the codewitchH Offline
          honey the codewitchH Offline
          honey the codewitch
          wrote on last edited by
          #25

          We at least agree on that much, bracing notwithstanding

          When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

          1 Reply Last reply
          0
          • honey the codewitchH honey the codewitch

            do you REALLY want me to enforce title casing for methods vs fields? because I can do that, and it makes my job a HELL of a lot easier.

            When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #26

            Privates get lower-case; all others get Pascal casing,

            1 Reply Last reply
            0
            • B BillWoodruff

              PIEBALDconsult wrote:

              The C pre-processor works just fine with C# code.

              That's very interesting; please say more.

              «One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #27

              For instance: Implanting Common Code in Unrelated Classes[^]

              B 1 Reply Last reply
              0
              • P PIEBALDconsult

                For instance: Implanting Common Code in Unrelated Classes[^]

                B Offline
                B Offline
                BillWoodruff
                wrote on last edited by
                #28

                Thanks, for your educational response ! The article, and comments/discussion, was interesting; imho, the technique you show is too esoteric for someone who is not C/C++ highly literate, like me. I am reminded, by your article, of using a factory class to spit out different flavors of stuff depending on input parameters/types. cheers, Bill

                «One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

                1 Reply Last reply
                0
                • honey the codewitchH honey the codewitch

                  foo("bar");

                  does this refer to an invocation of the method foo? or an invocation of the delegate instance referred to by the variable or argument foo? *headdesk* C# is massively ambiguous without having type information, so now I get to write a visitor to "patch up" the code dom with type information. So basically i always create these expressions as delegate invocations, but then i have to go back through later and find types, so I can change the right ones to method invocations instead. I know C family languages require type information to parse (which is unfortunate) but C# takes it to another level. In for a penny, in for a pound I guess.

                  When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                  J Offline
                  J Offline
                  JohaViss61
                  wrote on last edited by
                  #29

                  It is the C# equivalent of FUBAR. It means, just like the body bags coming from Vietnam, do not open. (the Class)

                  1 Reply Last reply
                  0
                  • honey the codewitchH honey the codewitch

                    foo("bar");

                    does this refer to an invocation of the method foo? or an invocation of the delegate instance referred to by the variable or argument foo? *headdesk* C# is massively ambiguous without having type information, so now I get to write a visitor to "patch up" the code dom with type information. So basically i always create these expressions as delegate invocations, but then i have to go back through later and find types, so I can change the right ones to method invocations instead. I know C family languages require type information to parse (which is unfortunate) but C# takes it to another level. In for a penny, in for a pound I guess.

                    When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                    S Offline
                    S Offline
                    Stuart Dootson
                    wrote on last edited by
                    #30

                    I have a suspicion that C++ is equally reliant (possibly more so when you get templates involved) on type information ```C++ int a(int x) { return x + 1; } auto b = [](int y) -> int { return y + 1; }; struct C { int operator()(int z) const { return z + 1; } }; C c; class D { int d(int x) const { return x + 1; } static int e(int x) { return x + 1; } public: void do_stuff(int a_number) { a_number = a(a_number); a_number = b(a_number); a_number = c(a_number); a_number = d(a_number); a_number = e(a_number); y = a_number; } int y; }; ``` 5 'callable things', all of which meet the [C++20 `invocable` concept](https://en.cppreference.com/w/cpp/concepts/invocable), but are of different types (Yes, `d` can only be called as if it has one parameter from the context of a method of `D` or a derived class, but that's the example context I've used!). And then if you use `if constexpr`, that will also impact how you interpret the program: ```C++ template struct bit_field_t { : : // Other methods : : auto get_value(uint32_t from_field) { if constexpr (Size == 1) { // Return a Boolean if Size == 1 return (field & (1<>Offset) & ((1< 1 | x -> x * fac x (* even and odd are inferred to have type int -> int *) let rec even n = match n with | 0 -> true

                    1 Reply Last reply
                    0
                    • honey the codewitchH honey the codewitch

                      do you REALLY want me to enforce title casing for methods vs fields? because I can do that, and it makes my job a HELL of a lot easier.

                      When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                      S Offline
                      S Offline
                      Stuart Dootson
                      wrote on last edited by
                      #31

                      Some languages *do* use casing to differentiate between different classes of name. Haskell requires types to start with an upper-case letter, values (and functions are just values of course) to start with a lower-case letter and if you stray away from that, a compiler error is raised. Rust has a convention with similar rules, but raises a warning not an error if you break it.

                      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                      honey the codewitchH 1 Reply Last reply
                      0
                      • L Lost User

                        #define then { #define endif } lets make C really basic so anyone can do it Edit: more complete #define IF if ( #define THEN ) { #define ELSE } else { #define ENDIF }

                        this internet has become nothing but fake news. ... time to fix it, time to get back to the fax!

                        E Offline
                        E Offline
                        englebart
                        wrote on last edited by
                        #32

                        Chanting: ELSEIF ELSEIF ...

                        1 Reply Last reply
                        0
                        • S Stuart Dootson

                          Some languages *do* use casing to differentiate between different classes of name. Haskell requires types to start with an upper-case letter, values (and functions are just values of course) to start with a lower-case letter and if you stray away from that, a compiler error is raised. Rust has a convention with similar rules, but raises a warning not an error if you break it.

                          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                          honey the codewitchH Offline
                          honey the codewitchH Offline
                          honey the codewitch
                          wrote on last edited by
                          #33

                          and i otherwise love haskell but i'm not a fan of that.

                          When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                          1 Reply Last reply
                          0
                          • honey the codewitchH honey the codewitch

                            foo("bar");

                            does this refer to an invocation of the method foo? or an invocation of the delegate instance referred to by the variable or argument foo? *headdesk* C# is massively ambiguous without having type information, so now I get to write a visitor to "patch up" the code dom with type information. So basically i always create these expressions as delegate invocations, but then i have to go back through later and find types, so I can change the right ones to method invocations instead. I know C family languages require type information to parse (which is unfortunate) but C# takes it to another level. In for a penny, in for a pound I guess.

                            When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #34

                            Action fooAction ...

                            It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                            honey the codewitchH 1 Reply Last reply
                            0
                            • L Lost User

                              Action fooAction ...

                              It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                              honey the codewitchH Offline
                              honey the codewitchH Offline
                              honey the codewitch
                              wrote on last edited by
                              #35

                              if you're suggesting my naming conventions aren't any good in the example I'd agree. The point however, is that it needs to parse regardless, or it's not C# :)

                              When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                              1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                honey the codewitch wrote:

                                does this refer to an invocation of the method foo? or an invocation of the delegate instance referred to by the variable or argument foo?

                                Neither: it means a VB programmer wants to access an array element and forgot where he was.

                                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                                S Offline
                                S Offline
                                Steve Naidamast
                                wrote on last edited by
                                #36

                                I don't see it that way and I am a VB.NET programmer but also fluent in C#. :laugh: It is amazing how many people really view C# as some completely different type of animal than that of VB.NET... To me, the line of code was calling the method, "foo", with a string parameter...

                                Steve Naidamast Sr. Software Engineer Black Falcon Software, Inc. blackfalconsoftware@outlook.com

                                N 1 Reply Last reply
                                0
                                • honey the codewitchH honey the codewitch

                                  foo("bar");

                                  does this refer to an invocation of the method foo? or an invocation of the delegate instance referred to by the variable or argument foo? *headdesk* C# is massively ambiguous without having type information, so now I get to write a visitor to "patch up" the code dom with type information. So basically i always create these expressions as delegate invocations, but then i have to go back through later and find types, so I can change the right ones to method invocations instead. I know C family languages require type information to parse (which is unfortunate) but C# takes it to another level. In for a penny, in for a pound I guess.

                                  When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                                  Z Offline
                                  Z Offline
                                  zezba9000
                                  wrote on last edited by
                                  #37

                                  "I know C family languages require type information to parse (which is unfortunate) but C# takes it to another level. in for a penny, in for a pound I guess." Static typing allows you to avoid runtime errors, its loads faster and much easier to find what is part of what. Sounds like you're coming from javaScript which teaches you how to think about things very wrong. A delegate in C# is a multicast-delegate in that when it invokes, its actually invoking a generated method that invokes the first function pointer, then the next and so on. It will always use virtual dispatch and can't be inlined. Don't use delegates for everything and only use them for what they're actually meant for.

                                  honey the codewitchH 1 Reply Last reply
                                  0
                                  • Z zezba9000

                                    "I know C family languages require type information to parse (which is unfortunate) but C# takes it to another level. in for a penny, in for a pound I guess." Static typing allows you to avoid runtime errors, its loads faster and much easier to find what is part of what. Sounds like you're coming from javaScript which teaches you how to think about things very wrong. A delegate in C# is a multicast-delegate in that when it invokes, its actually invoking a generated method that invokes the first function pointer, then the next and so on. It will always use virtual dispatch and can't be inlined. Don't use delegates for everything and only use them for what they're actually meant for.

                                    honey the codewitchH Offline
                                    honey the codewitchH Offline
                                    honey the codewitch
                                    wrote on last edited by
                                    #38

                                    No. I have a c++ background. I'm not talking about static versus dynamic, versus duck typing. I'm talking about parsing, an entirely different thing.

                                    When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                                    Z 1 Reply Last reply
                                    0
                                    • honey the codewitchH honey the codewitch

                                      No. I have a c++ background. I'm not talking about static versus dynamic, versus duck typing. I'm talking about parsing, an entirely different thing.

                                      When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                                      Z Offline
                                      Z Offline
                                      zezba9000
                                      wrote on last edited by
                                      #39

                                      ic, sry about that

                                      honey the codewitchH 1 Reply Last reply
                                      0
                                      • Z zezba9000

                                        ic, sry about that

                                        honey the codewitchH Offline
                                        honey the codewitchH Offline
                                        honey the codewitch
                                        wrote on last edited by
                                        #40

                                        It might be my fault. Let's see if I can explain better: Say I'm parsing C# and I encounter the following: Console.WriteLine(Int32.MaxValue); My parser can interpret Console as a field, property, method, variable or type. Any one of them is valid here, but we can't know which it is without having type information during the parse. Similarly: Also WriteLine(Int32.MaxValue) could be a delegate invoke on a field, or property reference, or it could be a method call. You can't know during the parse without type information. That's what i mean by needing type information during the parse. It's not that strong typing is bad. It's that requiring type information during a parse dramatically complicates parsing. What I do, is I create multiple trees on a single parse, and then resolve which tree it is after i have the type information for it. Some other parsers (like microsoft's research C# GLR parser) do that too, but it's not easy. The other option is to preparse, but only up to the member definitions (ignoring method bodies and such) and then parsing again once you have all that info.

                                        When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                                        Z 1 Reply Last reply
                                        0
                                        • honey the codewitchH honey the codewitch

                                          It might be my fault. Let's see if I can explain better: Say I'm parsing C# and I encounter the following: Console.WriteLine(Int32.MaxValue); My parser can interpret Console as a field, property, method, variable or type. Any one of them is valid here, but we can't know which it is without having type information during the parse. Similarly: Also WriteLine(Int32.MaxValue) could be a delegate invoke on a field, or property reference, or it could be a method call. You can't know during the parse without type information. That's what i mean by needing type information during the parse. It's not that strong typing is bad. It's that requiring type information during a parse dramatically complicates parsing. What I do, is I create multiple trees on a single parse, and then resolve which tree it is after i have the type information for it. Some other parsers (like microsoft's research C# GLR parser) do that too, but it's not easy. The other option is to preparse, but only up to the member definitions (ignoring method bodies and such) and then parsing again once you have all that info.

                                          When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                                          Z Offline
                                          Z Offline
                                          zezba9000
                                          wrote on last edited by
                                          #41

                                          Not something I've thought about much but Is this for a lesson you can't use Roslyn for? I'm not sure how Roslyn handles using-static/type-aliases.

                                          using static System.Console;
                                          namespace MyNamespace
                                          {
                                          class Program
                                          {
                                          private delegate void WriteLineFunc();
                                          private static WriteLineFunc WriteLine;

                                          	static void Main(string\[\] args)
                                          	{
                                          		WriteLine();// delegate is used
                                          	{
                                          {
                                          

                                          }

                                          So if a local, field/method isn't found mark the AST node as un-resolved or add it to an un-resolved list. Then after all files are parsed go through the un-resolved nodes and try to resolve them through the aliased types. Then you don't have to parse over and over if my thinking is correct.

                                          honey the codewitchH 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