سؤال

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

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top