Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C# have a equivalent to Objective-c's category?

Tags:

c#

objective-c

I'm looking for a equivalent to the Objective-c's Category for C# language.

like image 423
bollhav Avatar asked Apr 07 '11 10:04

bollhav


People also ask

How long can you have hep C without knowing?

Chronic liver disease in people with hepatitis C usually happens slowly, without any signs or symptoms, over several decades. Chronic hepatitis C virus infection is often not recognized until people are screened for blood donation or from an abnormal blood test found during a routine doctor's visit.

Does hep C go away?

Hepatitis C virus (HCV) causes both acute and chronic infection. Acute HCV infections are usually asymptomatic and most do not lead to a life-threatening disease. Around 30% (15–45%) of infected persons spontaneously clear the virus within 6 months of infection without any treatment.

How easy is it to get hep C?

Hepatitis C is spread only through exposure to an infected person's blood. High-risk activities include: Sharing drug use equipment. Anything involved with injecting street drugs, from syringes, to needles, to tourniquets, can have small amounts of blood on it that can transmit hepatitis C.

Does hep C treatment make you sick?

Like the other antivirals, the side effects are mild. You might have a slight headache or bellyache, or you might feel tired. Glecaprevir and pibrentasvir (Mavyret): Three pills daily can treat all types of hep C. Side effects are mild and can include headache, fatigue, diarrhea, and nausea.


2 Answers

You can't add methods to a class, however you can use extension methods to achieve similar effects.

make a static class, with a static method. The static methods first argument is marked with "this" and the method is decorated to the classes with the type of the argument.

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', '?' }, 
                             StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }   
}

This method will then be available on all instances of the type String. However you still have to have the extension class available through your usings.

The example is taken from Microsoft's own documention available here: http://msdn.microsoft.com/en-us/library/bb383977.aspx

like image 158
Morten Avatar answered Oct 07 '22 15:10

Morten


The closest thing to Objective-C Categories in C# is Extension Methods.

Note that C# is a statically typed language and doesn't use dynamic dispatching like Objective-C does. That means that method resolution is performed at compile time and not at runtime, like you are used to in Objective-C.

Related resources:

  • The Objective-C Programming Language: Categories and Extensions
  • Extension Methods (C# Programming Guide)
like image 21
Enrico Campidoglio Avatar answered Oct 07 '22 16:10

Enrico Campidoglio