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
0
Question by skoandi · Jun 01, 2013 at 12:01 PM · c#functiongetsetexplain

What are these things doing in C#?

I'm switching to C# from unityscript and so far I'm enjoying the ride :)

Can someone that knows c# well explain what these things are doing?

1: something = (float)sometingg; Why is "(type)" there? 2: In some functions, there's "get" and "set", what exactly does it do?

 float something{
     get{
     
     }
     set{
 
     }
 }
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
3
Best Answer Wiki

Answer by CorruptedHeart · Jun 01, 2013 at 01:16 PM

Edit: I misread the first question. See below for updated and correct answers

1: That is called casting, it is used to convert between two datatypes. (Not all datatypes can be converted automatically via casting) You would use this for example to convert between a float and an int.

 void Start()
 {
     float foo = 2.4f;
     int bar = 1;
     Debug.Log(Add(x, (float)y));
 }
 
 float Add(float x, float y)
 {
    return x + y;
 }

Another thing to watch out for is when doing calculations between integers that can result in a float value (dividing for example), you will need to cast one of the integers to a float otherwise it is taken as simply integer math, so the floating point part is simply dropped.

 int x = 10;
 int y = 3;
 
 float answer = x/y; // answer = 3  : incorrect if expecting floating point
 answer = x/(float)y; // answer = 3.333333  : correct if expecting floating point.

2: these are called properties, they are generally used to expose private variables. They can be used to perform validation on changes to variables. So if you wish to only be able to set a variable to a value between 1 and 100, it is possible to use properties to do that.

The get{} defines what gets returned when calling

 float something = theClass.something;

The set{} defines what happens when a value is assigned to the something property.

 float something = 10.0f;
 theClass.something = something;

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 skoandi · Jun 01, 2013 at 02:17 PM 0
Share

Thanks! Just what I needed :)

avatar image CorruptedHeart · Jun 03, 2013 at 08:39 PM 0
Share

Updated the post to be more correct due to misreading the question originally.

avatar image
3

Answer by Jamora · Jun 01, 2013 at 01:25 PM

1) that is a type cast, meaning that sometingg will be forced to be of type float. Read http://en.wikipedia.org/wiki/Type_conversion .

2) this is C# shorthand for getters and setters. Instead of creating methods like
public string GetName(){ return name;} public void SetName(string newName){ name = newName;} You type
public string Name{ get { return name;} set{ name = value;} }

Read more from http://msdn.microsoft.com/en-us/library/aa287786%28v=vs.71%29.aspx .

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 skoandi · Jun 01, 2013 at 02:17 PM 0
Share

Thank you for that example! :)

avatar image
0

Answer by falconfetus8 · Jun 01, 2013 at 03:14 PM

The types in parentheses are called "casts". Basically, it converts one datatype to another. Say I had an integer called "foo", and I had a function that only accepts floats as parameters. I would need to convert "foo" to a float in order to use it as a parameter. For example:

 void main(){
 
 int foo = 1;
 float sqrtOfFoo = SquareRoot((float)foo);
 
 //Since SquareRoot() requires a float as input, we need to "cast", or convert, foo as a float.
 
 }
 
 float SquareRoot(float input){
 return Mathf.Sqrt(input);
 }
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 skoandi · Jun 02, 2013 at 09:33 AM 0
Share

Really good example! Thanks! :)

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does my variable show a rising score AND 0 at the same time? 2 Answers

Are properties variables? 1 Answer

Multiple Cars not working 1 Answer

C# public parameter with custom get/set doesnt shown in the Inspector 4 Answers

Distribute terrain in zones 3 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