-4

So I started learning C# on Monday morning, I have spent the past two days watching video after video and now I have a few observations that I want to know if I am thinking about correctly.

My past is about ~4 years of college-level Java programming, so if any explanations are called for and referring it to a Java item is relevant, please feel free to make a mindset link to it.

I am also starting my first "real" application development internship, so they are having me learn stuff the first week on my own before I start working on C# and .Net based projects.

Any will be greatly appreciated!

  1. The "as" operator: From my understanding this is basically a cast. So I could say

    int a;
    double b;
    a as b;
    

    and this should be equivalent to (double)a, correct?

    or

    object sender;
    Label clickedLabel = sender as Label.
    

    This will then be like (Label)sender, with sender being an object but now casted as a label, correct?

  2. dynamic type: My understanding of this, is that it is a "all the things!" type, meaning that dynamic d; could be used as an int, double, object, string, ect.. it can apply to anything. Yes/no/kind of?

    dynamic d =5;
    int a = d;
    string b = d;
    object c = d;
    
  3. If that is correct, how is this different from boxing? Is boxing just an "as" operator/dynamic type but ONLY for use with the type Object?

  4. Integer Literals, how often are these used and in what scenarios? I watched a segment on them and they seen pretty strange. I will be doing application development so if these are useful please explain some of their everyday purposes please

    212 = decimal
    215 = decimal unsigned
    0xFccL = long hexidecimal (i think)
    058 = Illegal as 8 is not an octal digit
    05UU = Illegal as a suffix (the u for unsigned) is repeated
    30u1 = unsigned long
    30u = unsigned int
    

    I am just confused as to where these would all be used for an application development type. They seen more like direct assembly interaction to me, but maybe that is a common part of app dev stuff (first "real" internship)

  5. {#} syntax / WriteLine: So I have made a few demo apps and they use a syntax like..

    Console.WriteLine("Radius: {0}, Area: {1}", r, areaCircle);
    

    or

    Console.WriteLine("{0}:{1}:{2}", e.Hour.ToString(), e.Minute.ToString(), e.Second.ToString());
    

    From what I am realizing, the {0}, {1}, {2}, ect.. somehow are pulling values out of sets (that I do not think I physically created). I am unsure what this is "called" so I do not know how to Google for it.

    Would someone mind pointing me in the right direction as I find this to be a very useful operation.

  6. Nullable Types: From what I have learned here is that an expression such as

    static int? d;
    if (d.HasValue) 
    {
    // something
    }
    

    would not throw any errors as it would be a valid interaction, while in Java this would cause some kind of compile error.

    I believe you can also set a nullable type with an instantiation to begin with, such as:

     int? d = 5;
    

    Which would be the same as above but as a null attribute tagged to it.

    My question about this is,

    • Can "Nullable" be applied to all Types or only some?

    • Could you have an actual value for d, but still make a check return null? Since you can tag on a null attribute to a type, I am guessing that the attribute of "null" and the "value" itself are treated/checked independently of each other. Is that a correct or incorrect guess? (I can try to explain better if need be)

  7. string questions!

    • Do we used string in C# as we would use String in Java? (capitalization)

    • When using substring, why do these two strings return different answers?

      string sub = input.Substring(0, 3); //returns index 0-2
      
      string sub = input.Substring(3, 3); //returns index 3-6
      
  8. May add more...

    Hopefully this is not asking too much, I am just trying to make sure I learn C# as best as I can over the next few days.

    If any of you know some good articles/tutorials/ect on C# in terms of what a new app dev should know, please point me in that direction. I have a PluralSight account as well so you may direct me to their tutorials too.

    Thank you,

    -Austin

Austin
  • 3,010
  • 23
  • 62
  • 97
  • 1
    Have you touched any book or just watched some videos? These (and much more) questions are covered in books for learners (e.g. Troelsen's book). – Wiktor Zychla May 07 '14 at 17:06
  • 3
    You have lots of questions, but SO doesn't work well this way. You should ask each question you have separately, that way voting and accepting answers will work the way it's supposed to. – svick May 07 '14 at 17:06
  • 3
    Also, some of your “this should work, right?” samples won't actually compile. Before asking here, you should first try and see whether the code seems to work the way you expect. – svick May 07 '14 at 17:08
  • sounds like you should go get your hands dirty – Jonesopolis May 07 '14 at 17:08
  • I have watched close to 16+ hours of tutorial series so far, I do not have access to a book unfortunately. :/ – Austin May 07 '14 at 17:08
  • Just a note on `as`: It's slightly different than a cast. It returns `null` if the cast in invalid. This is usually used with reference types. A straight cast will throw an exception. – David Crowell May 07 '14 at 17:10
  • ask one question at a time. do you want from us to write a book for you? – Selman Genç May 07 '14 at 17:18
  • @svick All the code snippets (and I edited some just now) I have posted are either from tutorials, Microsoft's example sites, or have compiled now. But I will ask them separately if that's how SO is more geared. ty! – Austin May 07 '14 at 17:18
  • @selman22 I just figured a simple 1. yes, 2. yes, 3. no, go look at (some reference link), ect.. I can learn this stuff on my own, I just wanted to see if I am doing/thinking about this stuff correctly. – Austin May 07 '14 at 17:20
  • I actually think this is (barely) short enough to have a good SO answer. It is generally better to ask as separate questions though. – BradleyDotNET May 07 '14 at 17:21
  • `int a; double b; a as b;` will produce a compile-time error, for about 4 different reasons. – JLRishe May 07 '14 at 17:39

1 Answers1

0

I'll use your numbers in my answer:

  1. No, this is not equivalent. Using a "explicit" or "C-Style" cast will throw an invalid cast exception if the cast fails (say, if you try to cast a Label to an int). Using the as operator will return null if the cast fails, which is why you can't use it with a value type.

  2. dynamic means that it is a run-time determined type, and should be used carefully. C# is intended to be strongly-typed. Violate this at your own risk.

  3. "Boxing" is a whole different animal, and revolves around converting a value type to a reference type. See Boxing and Unboxing.

  4. Integer literals are used all the time. Say you want FF hex, you write 0xFF. You could write 255 of course, but the meaning is much clearer by using the literal.

  5. This is implicitly calling String.Format, a very powerful function. See MSDN for more information.

  6. Nullable applies only to value types (reference types are always nullable). Its not really a "tag" it just means the null value can be assigned to the variable. See is int? a value-type or a reference-type for more information.

  7. String and string are exactly the same thing. Your coding style determines which you use. To answer the second bullet, its because thats how the overload is defined to work. See String.Substring

  8. Please ask future questions as separate SO questions. Multi-parters like this are generally discouraged.

Finally, there are lots of great C# resources out there! MSDN should be a starting point, they have some pretty good documentation. For example, the top google hit for "Boxing C#" points to the linked article.

Community
  • 1
  • 1
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • 1
    “It effectively turns the value type into a reference type.” No, it doesn't. Nullable value types behave very differently from reference types when it comes to copying/assigning/passing them around. – svick May 07 '14 at 17:20
  • Thank you very much! I shall make-single question posts from now on. Sorry! – Austin May 07 '14 at 17:21
  • @svick, that statement was misleading, I apologize for that. I linked an appropriate SO question instead. – BradleyDotNET May 07 '14 at 17:22