Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need some explaination with beginner C# code

Tags:

c#

I have started to learn c#. I am trying to declare a class and some variables and trying to do a simple concatenation of strings. But i am getting some error - the code is below

namespace ConsoleApplication1
{
    class Class1
    {
        string s1 = "hi";
        string s2 = "hi";
        string s3 = s1 + s2;
    }
}

The error i am getting is - a field initializer cannot reference the non-static field, method, property 'ConsoleApplication1.Class1.s1

Can someone explain what is happening here.

Thanks.

like image 639
ravi Avatar asked Apr 24 '13 20:04

ravi


People also ask

What is C full explanation?

C Introduction C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What are basics of C?

It was mainly developed as a system programming language to write an operating system. The main features of the C language include low-level memory access, a simple set of keywords, and a clean style, these features make C language suitable for system programmings like an operating system or compiler development.

Can I learn C as a beginner?

Indeed, it is strongly recommended to start your programming journey with C language as it helps to understand a lot of underlying processes on the ground level, which enhances your fundamental knowledge & boosts your confidence, which further makes it easier for you to learn other high-level programming languages as ...


3 Answers

Can someone explain what is happening here.

Well, the compiler error message says it all, really, once you've got past the terminology. This line is invalid:

string s3 = s1 + s2;

You're declaring instance variables, and instance variable initializers (s1 + s2 here) aren't allowed to refer to other fields within the instance that's being created - or indeed the instance itself. Bear in mind that the above declaration is equivalent to:

string s3 = this.s1 + this.s2;

From section 10.5.5.2 of the C# 4 specification:

A variable initializer for an instance field cannot reference the instance being created. Thus it is a compile-time error to reference this in a variable initializer, because it is a compile-time error for a variable initializer to reference any instance member through a simple-name.

(Admittedly that's one of the more odder bits of wording in the spec...)

You have to put the logic into the constructor body instead:

class Class1
{
    string s1 = "hi";
    string s2 = "hi";
    string s3;

    public Class1()
    {
        s3 = s1 + s2;
    }
}
like image 179
Jon Skeet Avatar answered Oct 26 '22 10:10

Jon Skeet


Variables are not [logically] initialized in a particular order. It is best to design your programs such that they will work regardless of the order that the variables are initialized.

For non-trivial assignments of local variables you use a constructor for that type:

class Class1
{
    string s1 = "hi";
    string s2 = "hi";
    string s3;

    public Class1()
    {
         s3 = s1 + s2;
    }
}
like image 24
Servy Avatar answered Oct 26 '22 11:10

Servy


Try initializing s3 in a method, preferably a constructor

class Class1
{
    string s1 = "hi";
    string s2 = "hi";
    string s3;


    public Class1()
    {
        s3 = s1 + s2;
    }
}
like image 42
Sam I am says Reinstate Monica Avatar answered Oct 26 '22 11:10

Sam I am says Reinstate Monica