locked
Regarding Case Sensitivity in c# RRS feed

  • Question

  • Hi,

     

    I want to know why  c# is a case sensitive language and i hope code readability is not the only one reason for making c# language as case sensitive.

     

    I don't know abt the advantages of case sensitivity in c#. but i got some  problems becoz of that.

     

    eg :

     

    Im class Test, i declared two methods called getdata() and GETDATA()

     

    class Test

    {

    public string getdata()

    {

    return "stringone";

    }

    public string GETDATA()

    {

    return "stringtwo";

    }

    }

     

     

    i want to avoid the same method declaration twice with upper and lower cases.

     

    Regards

    Baji Prasad.B

    Tuesday, August 14, 2007 6:55 AM

Answers

  •  

    Well if you follow the well established .NET naming guidelines you'd name the method GetData, and the problem would never occur.

     

    You can use tools like FxCop to detect naming guideline voilations.

     

    Tuesday, August 14, 2007 11:32 AM
  •  

    I'll throw my two cents in here because I love language discussions. 

     

    In what way does the case of a method give the caller any indication of what the method does?  If I call GetData how do I know that the method does something different than GETDATA?  I don't.  Therefore case should be used to make names readable and not to convey some sort of meaning or purpose.  The name of a member should indicate its purpose.

     

    As someone had linked to, C# is based quite a bit off of C++ (with VB thrown in for good measure).  Still, in my experience, C++ developers pick up C# very easily while VB developers have a slightly longer learning curve because of the C-style symbolism.  Since C++ was case-sensitive so be it with C#.  So the question arises why was C (and C++) made case sensitive?

     

    I don't know that I've ever heard an official response from K&R about the case sensitivity issue but two benefits immediately come to mind.  Firstly is compilation speed (which was important back when C was invented).  Case insensitive comparisons are needed when a language is case insensitive.  This slows down a compiler quite a bit since the majority of time a compiler is dealing with arbitrary string tokens.  You can argue that you could just lower-case everything but you have to be careful because lowercasing non-ASCII languages can be tricky.  That is one reason why MS recommends against using upper/lowercase for comparison purposes.  Of course localization wasn't an issue back then but string comparisons were.  Lowercasing every string token would require two copies of every parsed string to be kept in memory because people get upset when they type in "Hello" and the compiler generates code for "hello".  As someone who use to write compilers for a living I can assure you QA doesn't find it half as funny as I do :}

     

    The other area is the available range of identifiers.  If we limit the character set to the letters 'a'..'z', 'A'..'Z' then a case sensitive language has 52 * N combinations of identifiers where N is the length of the identifier.  A case-insensitive language has only 26*N.  Now a days we (mostly) agree that case should not be used to distinguish symbols but back when C was invented limited memory mandated that smaller identifiers be used and therefore the more combinations you had the better.

     

    IMHO.

    Michael Taylor - 8/14/07

    http://p3net.mvps.org

     

    Tuesday, August 14, 2007 1:20 PM

All replies

  • Tuesday, August 14, 2007 7:33 AM
  • Hi karthikayen,

     

    I'm very thankfull for your quick reply.but as i mentioned in my question how can i avoid this below situation

     

    Im class Test, i declared two methods called getdata() and GETDATA()

     

    class Test

    {

    public string getdata()

    {

    return "stringone";

    }

    public string GETDATA()

    {

    return "stringtwo";

    }

    }

     

     

    i want to avoid the same method declaration twice with upper and lower cases.

     

     

     

    Regards

    Baji Prasad.B

    Tuesday, August 14, 2007 11:09 AM
  •  

    Well if you follow the well established .NET naming guidelines you'd name the method GetData, and the problem would never occur.

     

    You can use tools like FxCop to detect naming guideline voilations.

     

    Tuesday, August 14, 2007 11:32 AM
  •  

    I'll throw my two cents in here because I love language discussions. 

     

    In what way does the case of a method give the caller any indication of what the method does?  If I call GetData how do I know that the method does something different than GETDATA?  I don't.  Therefore case should be used to make names readable and not to convey some sort of meaning or purpose.  The name of a member should indicate its purpose.

     

    As someone had linked to, C# is based quite a bit off of C++ (with VB thrown in for good measure).  Still, in my experience, C++ developers pick up C# very easily while VB developers have a slightly longer learning curve because of the C-style symbolism.  Since C++ was case-sensitive so be it with C#.  So the question arises why was C (and C++) made case sensitive?

     

    I don't know that I've ever heard an official response from K&R about the case sensitivity issue but two benefits immediately come to mind.  Firstly is compilation speed (which was important back when C was invented).  Case insensitive comparisons are needed when a language is case insensitive.  This slows down a compiler quite a bit since the majority of time a compiler is dealing with arbitrary string tokens.  You can argue that you could just lower-case everything but you have to be careful because lowercasing non-ASCII languages can be tricky.  That is one reason why MS recommends against using upper/lowercase for comparison purposes.  Of course localization wasn't an issue back then but string comparisons were.  Lowercasing every string token would require two copies of every parsed string to be kept in memory because people get upset when they type in "Hello" and the compiler generates code for "hello".  As someone who use to write compilers for a living I can assure you QA doesn't find it half as funny as I do :}

     

    The other area is the available range of identifiers.  If we limit the character set to the letters 'a'..'z', 'A'..'Z' then a case sensitive language has 52 * N combinations of identifiers where N is the length of the identifier.  A case-insensitive language has only 26*N.  Now a days we (mostly) agree that case should not be used to distinguish symbols but back when C was invented limited memory mandated that smaller identifiers be used and therefore the more combinations you had the better.

     

    IMHO.

    Michael Taylor - 8/14/07

    http://p3net.mvps.org

     

    Tuesday, August 14, 2007 1:20 PM
  • Geeks,

          Each and every language has unique features. The C# uses the C,C++ stylish of coding , so it is follows the case-sensitive and C# allows C programming (unsfafe programming) too . I hope MS also think same way.

        As  question goes, Baji doesnt want use the language features. I assume he like to use the CLS features, enable the CLSCompliantAttribute in the class level  and add the line [assembly: CLSCompliantAttribute(true)] AssemblyInfo.cs file.

        [CLSCompliantAttribute(true)]
        public class Test
        {
            public string getdata()
            {
                return "stringone";
            }

            public string GETDATA()
            {
                return "stringtwo";
            }

        }


    At complie time you will following error:
    Class2.cs(16): Identifier 'xxx.xxx.GETDATA()' differing only in case is not CLS-compliant

    Thanks & Regards,
    Siva




        
       
    Friday, August 17, 2007 7:16 AM
  •  

    Just be aware that using CLSCompliant on a type/member has more impact than just disabling case-sensitivity.  For example you'll now get errors whenever you use sbyte or ulong.  Such is the life of CLS compliant code.  Also be aware that you are alienating a large portion of the .NET community since they use C++ or C#.  In order to be able to use same named functions with different cases in these languages you'll have to provide alternatives anyway so why bother. 

     

    Finally it is strictly a coding design rule that most people adhere to that case has no relevance in the meaning of a function.  No amount of attributes or language conformance is going to help you when you are faced with getdata and GETDATA and you don't know what the differences are.  Even the difference between rate and RATE as local variables is all but meaningless to anyone besides the original author.  IMHO.

     

    Michael Taylor - 8/17/07

    http://p3net.mvps.org

     

    Friday, August 17, 2007 12:38 PM