property inference

R

rodchar

hey all,

given:

class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return this.FirstName + " " + this.LastName;
}
}

question:
so, if you do this type of inference there's no access to the private
variable? is that correct?

thanks,
rodchar
 
H

Hans Kesting

rodchar brought next idea :
hey all,

given:

class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return this.FirstName + " " + this.LastName;
}
}

question:
so, if you do this type of inference there's no access to the private
variable? is that correct?

thanks,
rodchar

It's not "inference" as you specify the types explicitly. The term
"inference" is used for statements like:
var s = "something";

where the compiler infers that the variable "s" should be of type
"String".


This:
public string FirstName { get; set; }

is a "automatically implemented property". In reality the compiler
generates a backing-field for you, but you can not access that
directly.


Hans Kesting
 
B

Ben Voigt [C++ MVP]

rodchar said:
hey all,

given:

class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return this.FirstName + " " + this.LastName;
}
}

question:
so, if you do this type of inference there's no access to the private
variable? is that correct?

What inference? What private variable? The code you posted has neither.
 
P

Peter

Ben said:
What inference? What private variable? The code you posted has
neither.

I guess the compiler "infers" that the properties FirstName and
LastName should have a private backing variable, and that it should
generate the code to support getting and setting these values?

(This use of the term "inference" of course conflicts with another,
usual, use of the term "inference" in c#).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top