0

Consider, class test

Step 1-->

  public class test
    {
    private string temp_err=string.Empty;

    public static void GetResult()
    {
     temp_err="abc";  
    }

    }

Step 2-->

 public class test
        {

        private String temp=string.Empty;

        public static void GetResult()
        {
         temp="abc";  
        }

        }

In case of Step 1 an error is coming: Cannot access non-static field in static context.

but when I've changed my code like in case of Step 2 everything is working fine.

Kindly explain what is the difference between string and String..

On google, it is written that string is the alias of System.String which describes there is no such difference.

Please guys before selecting this question in possible duplicate category. Read all the 41 answers given on that url and then take some decision. I'm asking about there behavior in Static method. Please read the question again

Tanul
  • 354
  • 4
  • 20
  • 10
    _in case of Step 2 everything is working fine_ I don't think so. It should give an error also. – Soner Gönül Sep 17 '14 at 08:05
  • possible duplicate of [What's the difference between String and string?](http://stackoverflow.com/questions/7074/whats-the-difference-between-string-and-string) – Sayse Sep 17 '14 at 08:10
  • 2
    When the code contains more than one error, a compiler might not show all errors. If you correct the error in the line `Step 1` (by removing that line entirely), you will see, like @SonerGönül said, that the line 2 gives the same error. – Jeppe Stig Nielsen Sep 17 '14 at 09:30
  • Jeppe Stig Nielsen: I think now you'll get my query.. – Tanul Sep 17 '14 at 09:37

3 Answers3

0

You have to make fields static to be able to access in static method, you can not pass non-static fields to a static method:

private static string temp_err=string.Empty;
private static String temp=string.Empty;
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0

There is no difference between string and String, but in the cases where you are calling a static method of the string class it may be considered better style to use the class name String such as String.Format.

Jaycee
  • 3,098
  • 22
  • 31
-1

As you already found out, there is no implementation difference between string and String as string is just an alias for the .NET class System.String. This means it is a C# language keyword.

Using string as a language keyword makes it appear like a C# build in type (in VisualStudio is will be colored like the other C# keywords). In fact, though being a class object, strings are somewhat special in that the compiler uses value semantics for comarision to make it more intuitive.

Regarding your code: The second case has the same error as in the first case. Maybe in your real code you have a local or global variable that is also named temp, that shadows your memeber variable and therefore it doesn't generate an error.

hManthey
  • 34
  • 3
  • I've created the whole code in my project about half an hour ago. I don't think so the reason you've given is valid at all. If a person is asking this type of question then at least he should be knowing whether he written any local or global variable or not. – Tanul Sep 17 '14 at 09:13
  • Your question was that someone would explain the difference between string and String. My suggestions about why the second case may compile without error are just possible answers, as your code doesn't show any other reason to me. – hManthey Sep 17 '14 at 09:17