Post: [C#] Implicit Operators
09-21-2015, 11:57 AM #1
Winter
Purple God
(adsbygoogle = window.adsbygoogle || []).push({}); I'm sitting here thinking, what can I do since I'm bored as fuck but too lazy to make something. What I came up with was a little more advanced C# tutorials. Most people don't use classes / structures in their tools/menus but what the hell I'll make this tutorial anyway.

Implicit Operators.
What is an implicit operator? Instead of using casting and hoping that the class already has a method to deal with your conversion (when dealing with structures chances are that there isn't a method) so in C# we have implicit operators.


How are they used?
An implicit operator can be used like this (an example grabbed from my NGU API)

    
[Serializable]
struct BBText {
public string Body;

public bool Bold;
public bool Underline;
public bool Italic;
public bool Strikeout;


public BBText(string body = "", bool bold = false, bool underline = false, bool italic = false, bool strikeout = false) {
Body = body;
Bold = bold;
Underline = underline;
Italic = italic;
Strikeout = strikeout;
}

public static implicit operator string (BBText bbtext) {
return bbtext.Body;
}
}
//I removed the Serialize methods to cut it down to size


so instead of doing
     string messageText = new BBText("A message probably in the shoutbox").Body; 


by adding the string class on the implicit operator we've made an implicit conversion for the compiler to use.

For example purposes, how can I make my own?
    
struct ExampleStructure {
public bool ExampleBool;
public string ExampleString;
public int ExampleInt;

public static implicit operator bool (ExampleStructure examplestructure) {
return examplestructure.ExampleBool;
}
public static implicit operator string (ExampleStructure examplestructure) {
return examplestructure.ExampleString;
}
public static implicit operator int (ExampleStructure examplestructure) {
return examplestructure.ExampleInt;
}
}


now we can do
    
ExampleStructure es = new ExampleStructure() { ExampleBool = true, ExampleString = "Hello", ExampleInt = 10 };

bool _bool = es.ExampleBool;
string _string = es.ExampleString;
int _int = es.ExampleInt;


That's cool and all, but how does that help me?
It's more efficient by not having to look for a method that isn't explicit for that conversion and also makes your code look A LOT more neater which is good to practice neat and efficient code.


No one will probably give a shit about this anyway, but if I was a programming teacher and you were using more advanced methods like structures, classes, explicit and implicit operators, initializers etc I would give you extra points for not being a fuckwit.
Enjoy your new found knowledge that you'll never use.

The following 2 users say thank you to Winter for this useful post:

Adrian
09-21-2015, 03:46 PM #2
Mango_Knife
In my man cave
Originally posted by Winter View Post
No one will probably give a shit about this anyway, but if I was a programming teacher and you were using more advanced methods like structures, classes, explicit and implicit operators, initializers etc I would give you extra points for not being a fuckwit.
Enjoy your new found knowledge that you'll never use.


Acctually
I'm using this all the time when coding, it making the code much more cleaner and nicer
And instead of using the casting (expilicit), im using this wich it's much nicer and easier

Hmm, but when what is the "Serializable" attribute for?
09-22-2015, 12:13 AM #3
Winter
Purple God
Originally posted by Knife View Post
Acctually
I'm using this all the time when coding, it making the code much more cleaner and nicer
And instead of using the casting (expilicit), im using this wich it's much nicer and easier

Hmm, but when what is the "Serializable" attribute for?


Oh, probably should've removed that, but it makes the object serializable so for example, if I serialize it into bytes (using Marshal) and then encode the bytes into text save the .txt file decode the text into bytes and deserialize the bytes into BBText. But I ended up making my own serialize method since data can be lost when using the serializable attribute. It's just nice to have

The following user thanked Winter for this useful post:

Mango_Knife

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo