Jump to content

Ad Block detected  

Welcome To The Home Of
AspectModding

Extra discount

  • Chaotic360 sales are now on
  • 20$ For lifetime premium 
  • Limited time only

AspectModding is currently under maintenance if you discover any bugs and/or errors report them to a member of staff  right away!

To become a beta tester of Chaotic360 Remastered V3.0.0 Message ZZ9 x iHaXoRZz or ZZ9 x iCalZz

  • Announcements

    • ZZ9 x iHaXoRZz

      Chaotic360 Beta Version is now available   12/16/17

      If you are interested in becoming one of our beta testers  you will need the following. Access to Xbox live Have Xbox Live Gold  Have all of the Call Of Duties Ban Bypasses  If you have all of what's needed, then you are one step closer to becoming a Beta Tester, Once you are a Beta Tester you job will be testing all of the options of Chaotic360 old and new, and reporting back anything you believe that could be fixed or improved, this includes slow performance, fatal crashes, console freezing, spelling mistakes etc.  
Sign in to follow this  
distancexd

[c#] Extensions

Recommended Posts

What are extensions?

  • An extension method enables you to add methods to existing types without creating a new derived type, recompiling, or modify the original types. We can say that it extends the functionality of an existing type in .NET. An extension method is a static method to the existing static class. We call an extension method in the same general way; there is no difference in calling.

 

The following list contains basic features and properties of extension methods

  • It is a static method.
  • It must be located in a static class.
  • It uses the "this" keyword as the first parameter with a type in .NET and this method will be called by a given type instance on the client side.
  • It also shown by VS intellisense. When we press the dot (.) after a type instance, then it comes in VS intellisense.
  • An extension method should be in the same namespace as it is used or you need to import the namespace of the class by a using statement.
  • You can give any name for the class that has an extension method but the class should be static.
  • If you want to add new methods to a type and you don't have the source code for it, then the solution is to use and implement extension methods of that type.
  • If you create extension methods that have the same signature methods as the type you are extending, then the extension methods will never be called.

How do I create an extension in c#?



    public static class MyExtension



    {



        public static void MyMethod(this <type> <name>, <extra objects>)



        {







        }



    }

__________________________________________________

Example



public static int CountVowels(this string str)



{



    char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };



    int vowelCount = 0;



    for (int i = 0; i < str.Length; i++)



    {



        char current = str[i];



        if (vowels.Any(x => current == x))



        {



            vowelCount = vowelCount + 1;



        }



        return vowelCount;



    }



}



Usage



int vowels = "some random string".CountVowels();

__________________________________________________

Example w/ additional input.



public static bool EqualsIgnoreCase(this string str, string equals)



{



    return str.ToLower() == equals.ToLower();



}

Usage



bool equals = "RANDOM STRING".EqualsIgnoreCase("RaNdOm StRiNg");

__________________________________________________

~the descriptions were not written by me but the code examples were.

Edited by privatebool

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

About us

Welcome to the home of AspectModding where we offer
you your daily mods to your general gossip, if your into
gaming, modding, programming, or just looking for new
friends this is the place for you

Follow us

×