Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by UniteMage · May 24, 2011 at 02:18 AM · c#staticnon-static

C# Going Static or Going OOP

I have some problems with the "static" thinking in C#... It seems it is not legal to access a static method or member variable through an instance.

public FSM fsm;

instance -> fsm.transition();

static -> FSM.SomeMethod();

Question for C# Unity Users - How does one know for example in the Unity Engine which methods or members are static and those that are instantiated?

Thanks CSDG

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by CHPedersen · May 24, 2011 at 06:45 AM

Console is actually a public sealed class, but Console.ReadLine() is a static method. C# doesn't strictly support singleton classes in the sense that there's a keyword (like Java's singleton keyword), but you can implement the pattern manually. For those interested, Microsoft explains how in this article.

It's interesting that you should point out the ambiguity between instance access and static access, I'd never realized that, as in your example with A.B(). You see, you're never really in doubt if you use an editor with proper syntax highlighting. Here's an example with a bit of code I zoomed in on, in Visual Studio 2010:

Syntax highlighting

Notice the light blue color? Everything colored light blue is statically accessed. If it's black, it's variable access. I.e. Util.ToolDepthLabelScript.SetTextSize(); is an access to the method SetTextSize() on the variable ToolDepthLabelScript (a script instance), located in the static class Util. The highlighting makes it really simple.

Comment
Add comment · Show 1 · 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 UniteMage · May 24, 2011 at 07:38 PM 0
Share

I thought that the code coloring for editors are based on color rules and Code Templates. The members of a class can be either instance members or static members. You access a static member through the name of the class in which it is declared just as you would if it were instance member. So I guess it is not possible to distinguish a static member nor an instance member in the Unity Engine because all class names are highlighted.

Thanks CSDG

avatar image
0

Answer by Owen-Reynolds · May 24, 2011 at 06:03 AM

The docs list a var/function as a class function if it is a static, which seems like pretty standard C# convention.

C# really is confusing that way, it's not a Unity thing. In C++, regular functions are probably in a namespace, and you use Mathf::sqrt(9);. The :: lets you know that Mathf is just the scope.

Whereas C# (not just Unity) encourages you to group regular functions in "fake" classes, full of only statics and no member vars, which will never be instantiated. You can never tell in A.B() if A is a variable or a scope. I always assumed it was a design issue: I'm not sure if Console if a singleton or a class, but I use Console.ReadLine() either way.

Comment
Add comment · Show 1 · 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 UniteMage · May 24, 2011 at 07:47 PM 0
Share

I should have given a better clarification in order to deter$$anonymous$$e if there was a way to distinguish the difference between the different types of classes and their members. I am fascinated with C#. I behaves like C++ but has a lot of characteristics in Java. With all things considered, the question was not that important.

avatar image
0

Answer by ckfinite · May 24, 2011 at 08:00 PM

I normally rely on naming conventions, i.e. fsm is a field, whereas Fsm is a class. What does cause problems is when you have a public property of, for example, a Rectangle that is called Rectangle. In this case, you would have to use either highlighting or looking at statically available functions. VS does make this really clear, IntelliSense is much better than normal rules.

Comment
Add comment · Show 2 · 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 UniteMage · May 24, 2011 at 08:42 PM 0
Share

I am trying to get a handle on $$anonymous$$onoDevelop as well as everything. I don't want to seem paranoid but it seems that some are inclined lay down misinformation or inclined to lay down principles as incontrovertibly true. Please.

avatar image ckfinite · May 24, 2011 at 09:26 PM 0
Share

$$anonymous$$onoDevelop is much better for C# dev than Unitron/UniScte. You syntax highlighting will work in a similar way to VS's.

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

An object reference is required to accsess a non-static member 'Lives.loselife()' 1 Answer

Calling non-static from static function C# 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Save static fields values 1 Answer


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