C#
  Home arrow C# arrow Destroying Objects in C#
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Destroying Objects in C#
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2009-11-02

    Table of Contents:
  • Destroying Objects in C#
  • Using Static Fields
  • Destroying Objects
  • Destructors Versus Dispose
  • Passing Parameters

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Destroying Objects in C#


    (Page 1 of 5 )

    In this third part of a four-part series on classes and objects in C#, you'll learn how to invoke static methods, how to use destructors, and more. This article is excerpted from chapter four of the book Programming C# 3.0, fifth edition, written by Jesse Liberty and Donald Xie (O'Reilly, 2008; ISBN: 0596527438). Copyright © 2008 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    Invoking Static Methods

    The Main() method is static. Static methods are said to operate on the class, rather than on an instance of the class. They don’t have a thisreference, as there is no instance to point to.

    Java programmers take note: in C#, calling static methods through instance variables is not permitted.

    Static methods can’t directly access nonstatic members. ForMain()to call a nonstatic method, it must instantiate an object. Consider Example 4-2, shown earlier.

    SomeMethod()is a nonstatic method ofMyClass. ForMain()to access this method, it must first instantiate an object of typeMyClass, and then invoke the method through that object.

    Using Static Constructors

    If your class declares a static constructor, you are guaranteed that the static constructor will run before any instance of your class is created.*

    You can’t control exactly when a static constructor will run, but you do know that it will be after the start of your program and before the first instance is created. Because of this, you can’t assume (or determine) whether an instance is being created.

    For example, you might add the following static constructor to theTime class from Example 4-4:

      static Time()
      {
        Name = "Time";
      }

    Notice that there is no access modifier (e.g.,public) before the static constructor. Access modifiers aren’t allowed on static constructors. In addition, because this is a static member method, you can’t access nonstatic member variables, and soNamemust be declared a static member variable:

      private static string Name;

    The final change is to add a line toDisplayCurrentTime(), as in the following:

      public void DisplayCurrentTime()
      {
        System.Console.WriteLine("Name: {0}", Name);
        System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
        Month, Date, Year, Hour, Minute, Second);
      }

    When all these changes are made, the output is:

      Name: Time
      11/27/2007 7:52:54
      Name: Time
      11/18/2007 11:45:30

    (Your output will vary depending on the date and time you run this code.)

    Although this code works, it isn’t necessary to create a static constructor to accomplish this goal. You can, instead, use an initializer:

      private static string Name = "Time";

    which accomplishes the same thing. Static constructors are useful, however, for setup work that can’t be accomplished with an initializer and that needs to be done only once.

    Java programmers take note: in C#, a static constructor will serve where a static initializer would be used in Java.

    For example, assume you have an unmanaged bit of code in a legacy DLL. You want to provide a class wrapper for this code. You can callLoadLibrary in your static constructor and initialize the jump table in the static constructor. I discuss handling legacy code and interoperating with unmanaged code in Chapter 22.

    Static Classes

    In C#, there are no global methods or constants. You might find yourself creating small utility classes that exist only to hold static members. Setting aside whether this is a good design, if you create such a class, you won’t want any instances created. Mark your classStaticto ensure that no instance of the class may be created. Static classes are sealed, and thus you may not create derived types of aStaticclass. Note, however, that static classes may not contain nonstatic members or have a constructor.

    More C# Articles
    More By O'Reilly Media


     

    C# ARTICLES

    - C# Meets Design Patterns
    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 8 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek