c++ ##_##
using System;
class Employee
{
private int empid;
public Employee()
{
empid = 101;
}
public int EmpId
{
get
{
return empid;
}
}
}
class program
{
static void Main(string[] args)
{
Employee emp = new Employee();
emp.EmpId = 102;
Console.WriteLine(emp.EmpId);
}
}
Correct answer: 4
Syntax error
The above code snippets will generate error, because it is read only property.
The output would be,
using System ;
class Employee
{
private int empid ;
public Employee ( )
{
empid = 101 ;
}
public int EmpId
{
get
{
return empid ;
}
}
}
class program
{
static void Main ( string [ ] args )
{
Employee emp = new Employee ( ) ;
emp . EmpId = 102 ;
Console . WriteLine ( emp . EmpId ) ;
}
}
正确答案:4
语法错误
上面的代码段将产生错误,因为它是只读属性。
输出将是
using System;
class Employee
{
private int empid;
public int EmpId
{
get
{
return empid;
}
}
}
class program
{
static void Main(string[] args)
{
Employee emp = new Employee();
Console.WriteLine(emp.EmpId);
}
}
Correct answer: 2
0
The code snippet will print "0" on the console screen.
Correct answer: 1
value
To set value in data members, we need to use value keyword in the set accessor.
Correct answer: 4
no need to use any keyword
In get accessor, there is no need to use any keyword.
using System;
class Employee
{
private int empid;
public Employee()
{
empid = 101;
}
private int EmpId
{
get
{
return empid;
}
set
{
empid = value;
}
}
}
class program
{
static void Main(string[] args)
{
Employee emp = new Employee();
emp.EmpId = 102;
Console.WriteLine(emp.EmpId);
}
}
Options:
Correct answer: 4
Syntax error
The above code snippets will generate error because we cannot access private property outside the class.
翻译自: https://www.includehelp.com/dot-net/csharp-properties-aptitude-questions-and-answers-3.aspx
c++ ##_##