dcsimg
Results 1 to 5 of 5

Thread: The "static" modifier?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    The "static" modifier?

    As I'm learning C#, a lot of concepts are familiar to me. However, there is one that really eludes me. That is the "static" modifier. I have found many explanations of what the "static" modifier is, however, it's still not clicking. Since I'm a visual person, I need to see it in action and what it actually does or prevents from happening. My apologies but sometimes "The lights are on but nobody's home." Could someone please show me a realistic coding example of the "static" modifier in action?

    Thanks,
    Blake

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,583

    Re: The "static" modifier?

    In what context? You can have Static methods and static variables. You can also have static classes.
    Are you more familiar with VB.Net? Have you used Classes with Shared methods and variables? Those would be essentially the same as Static methods and variables in C#.
    A VisualBasic Module would be like a C# Static Class.

    If you need examples, you've probably used some already.
    Have you ever used Math.Pi or Math.Sin?

    You didn't have to create an instance of the Math class, i.e. Math myMath = new Math();, and then use myMath everywhere to access those methods.
    Math is a static class, so you don't create an instance of the class, you use the class itself. Likewise all its methods and variables are declared static.

    If you had a static variable defined in a class that wasn't static, then when you created instances of that class, all the classes would be sharing that static variable so you wouldn't change it using the instance name.

    This link describes the use of static, and gives example code to try. I don't think I need to reiterate it all here. Check it out and see if it clicks.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    Re: The "static" modifier?

    passel,

    I checked out the link and that explained it pretty well with the code example. What I gained from it is that you can't instantiate a class. If that's the case, then what is the advantage of using a static class as opposed to just accessing a static class directly like the example showed?

    Thanks,
    Blake

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,788

    Re: The "static" modifier?

    A static member (method, property, etc) in a non-static class in C# is exactly like a Shared member in a class in VB. A static class in C# is pretty much exactly like a module in VB. In fact, a VB module becomes a static class when compiled. The differences between a C# class and a VB module are only at the language level:

    1. In a C# static class, all members must be declared static. In a VB module, you don't declare each member Shared but you invoke them in the same way as you do Shared members of classes.
    2. Members of a C# static class must be qualified by the class name when invoked. Members of a VB module can be invoked without qualification, unless it is required for disambiguation, i.e. determining which of multiple members with the same name is to be invoked.
    3. VB modules can't have constructors.

    The fact that C# static classes and VB modules are equivalent is exemplified by the fact that extension methods can only be declared in static classes in C# and only in modules in VB.

    In short, if you already understand how modules and Shared members work and are used in VB, you also already know how static classes and static members work and are used in C#. If you're not asking why use a module instead of a class with Shared members in VB, there's no reason to be asking why use a static class instead of a non-static class with static members in C#. Basically, if there's no good reason to instantiate a type, i.e. every member is static or Shared, then you should declare that type as static or as a module so that it cannot be instantiated. It's much like keeping the scope of a variable as narrow as possible: you don't have to do it but why allow access to something that cannot serve a useful purpose if you can easily prevent it.

    It's also worth noting that some VB developers have an aversion to using modules while no such aversion to static classes exists among C# developers. This is evidence that that aversion among VB developers is due to the perception that modules are specifically a VB6 thing. They are not. They look like VB6 modules for the most part, but their implementation is 100% .NET.
    Last edited by jmcilhinney; May 25th, 2018 at 10:07 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,832

    Re: The "static" modifier?

    Thanks jmc...that was a great explanation. It makes sense now.
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width