Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by flamy · Jun 13, 2012 at 09:53 AM · performanceoptimizationclassstatic

General programming question on Static Classes

Hello this is not anything related to unity, just a general programming question

I am having a this doubt about the static classes and static function, still im unable to find which of the following is better at which situation.

say there are 2 classes,

 public class A
 {
 
   public static void doSomething()
   {
     //
   } 
 }
 
 
 public static class B
 {
   public static void doSomething()
   {
     //
   } 
 }


now if i call A.doSomething() and B.doSomething, what will be the difference, which one will be faster. And which one will be effective. Well is there any difference at all actually?

I have tried to figure it out for quiet some time now, but helpless here.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by whydoidoit · Jun 13, 2012 at 10:10 AM

There is no difference in performance. A static class declares that all of its members are static, and the compiler enforces that.

The one benefit of static classes is that you can use them to write Extension Methods.

  public static class A
  {
        public static void CoolHuh(this GameObject go)
        {
               go.transform.forward = go.transform.right;
               Debug.Log("Not actually that cool");
        }
  }

Then you can do

   anyGameObject.CoolHuh();
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image flamy · Jun 13, 2012 at 10:14 AM 2
Share

hey thanks a lot and also thanks for introducing me to extension methods, that is what i actually wanted to do.

and one more thing, isnt the static class loaded to the memmory as soon as the namespace is included?? then what about a normal class with all static methods? even that is the same??

avatar image Bunny83 · Jun 13, 2012 at 10:26 AM 2
Share

They behave exactly the same. Even a normal class can have an additional static constructor and static variables which are initialized when you access the class the first time. There's really no difference except that the compiler forces you to make everything static.

avatar image whydoidoit · Jun 13, 2012 at 10:27 AM 0
Share

Are you sure about that? I'm pretty sure the static constructor of a normal class that has no instance created is not called.

Given that the other comments may be hidden, I should just clarify that this comment was made before an edit of Bunny's reply above

avatar image whydoidoit · Jun 13, 2012 at 10:28 AM 1
Share

But i have been known to be wrong :)

avatar image Bunny83 · Jun 13, 2012 at 10:55 AM 1
Share

Yes, i'm sorry, the static field initializer and the static constructor are called when ever you access any thing of that class. If you read a static variable for example the system will call the field initializers first and then the static constructor if the class hasn't beed already initialized ;)

I've changed my comment above

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Are .gameObject and .transform both using GetComponent() in the background? 1 Answer

Add 2d rigidbodies to moving objects to increase performance or not? 1 Answer

Couroutines not working in static class 1 Answer

Increase Unity2D editor performance 0 Answers

(2 hidden skinned meshes) or (1 extra draw call) for character props? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges