Post: [C#] Class extension methods
10-24-2015, 11:28 AM #1
Winter
Purple God
(adsbygoogle = window.adsbygoogle || []).push({}); Just thought this could be of use to someone who likes neat code :p

First off, we need to make a new class. Call it "Extensions.cs" or whatever you want to call it, just make sure it's a class

when you're in the class file (Extensions.cs) you'll see where it says "class Extensions"
change "class" to "static class". This is crucial.


Now making the actual extension methods.
In the static class you just made, for demonstration purposes I'll be making a thread start that returns the instance of Thread when it starts.


    
/*The "this Thread t" is probably the most important part of this bit, you want your [i]first[/i] argument to be "this CLASSTYPE qualifier"*/
public static Thread StartThread(this Thread t) {
t.Start();
return t;
}


So what did this do?
Well in the class "Thread" we added another method called StartThread which has a type of Thread. What we can do with that is this;
    
var thread = new Thread(_ => {
Console.WriteLine("This is from another Thread!");
}).StartThread();


Just so you get the idea, I'll do another one.
this time we'll be making a custom split, this is useful since we don't have to initialize array in our function everytime (makes it bulky)

    
public static string[] Split(this string s, string delimiter) {
return s.Split(new string[] { delimiter }, StringSplitOptions.None);
}


now we can do

    
string splat = "1~_~2".Split("~_~")[0];

Output: 1

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

Devious, FRINZ
10-25-2015, 04:40 AM #2
FRINZ
I’m too L33T
1st nigga
10-25-2015, 10:17 AM #3
Mango_Knife
In my man cave
Originally posted by Winter View Post
Just thought this could be of use to someone who likes neat code :p

First off, we need to make a new class. Call it "Extensions.cs" or whatever you want to call it, just make sure it's a class

when you're in the class file (Extensions.cs) you'll see where it says "class Extensions"
change "class" to "static class". This is crucial.


Now making the actual extension methods.
In the static class you just made, for demonstration purposes I'll be making a thread start that returns the instance of Thread when it starts.


    
/*The "this Thread t" is probably the most important part of this bit, you want your [i]first[/i] argument to be "this CLASSTYPE qualifier"*/
public static Thread StartThread(this Thread t) {
t.Start();
return t;
}


So what did this do?
Well in the class "Thread" we added another method called StartThread which has a type of Thread. What we can do with that is this;
    
var thread = new Thread(_ => {
Console.WriteLine("This is from another Thread!");
}).StartThread();


Just so you get the idea, I'll do another one.
this time we'll be making a custom split, this is useful since we don't have to initialize array in our function everytime (makes it bulky)

    
public static string[] Split(this string s, string delimiter) {
return s.Split(new string[] { delimiter }, StringSplitOptions.None);
}


now we can do

    
string splat = "1~_~2".Split("~_~")[0];

Output: 1


Extensions methods are really something!
Every time i'm programming something im using them
And it make's life much easier
            public static void RandomColor(this Label L)
{
R = Rand.Next(0, 200);
G = Rand.Next(0, 220);
B = Rand.Next(0, 200);
A = Rand.Next(200, 255);
L.ForeColor = Color.FromArgb(A, R, G, B);
}
10-25-2015, 12:38 PM #4
Winter
Purple God
Originally posted by Knife View Post
Extensions methods are really something!
Every time i'm programming something im using them
And it make's life much easier
            public static void RandomColor(this Label L)
{
R = Rand.Next(0, 200);
G = Rand.Next(0, 220);
B = Rand.Next(0, 200);
A = Rand.Next(200, 255);
L.ForeColor = Color.FromArgb(A, R, G, B);
}


Why not just make it even shorter and do

    
L.ForeColor = Color.FromArgb(new Random().Next(255), new Random().Next(255), new Random().Next(255), new Random().Next(255));


~edit: The random class doesn't initialize anything that takes more than 10ms so there's no point trying to efficiently do it, unless you wanted to make a signle rand var I guess would be the limit of efficiency
10-25-2015, 12:54 PM #5
Mango_Knife
In my man cave
Originally posted by Winter View Post
Why not just make it even shorter and do

    
L.ForeColor = Color.FromArgb(new Random().Next(255), new Random().Next(255), new Random().Next(255), new Random().Next(255));


~edit: The random class doesn't initialize anything that takes more than 10ms so there's no point trying to efficiently do it, unless you wanted to make a signle rand var I guess would be the limit of efficiency


Haven't thought of using "Color.FromArgb".

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo