GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  C#
Go To First  |  Previous Question  |  Next Question 
 C#  |  Question 390 of 436    Print  
Is it possible to have different access modifiers on the get/set methods of a property? Justify

  
Total Answers and Comments: 11 Last Update: December 02, 2008     Asked by: nandhini 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
December 14, 2007 06:22:22   #1  
ranjik2008 Member Since: December 2007   Contribution: 3    

RE: Is it possible to have different access modifiers on the get/set methods of a property? Justify
Yes It is possible to have different access modifiers but the best approach is to use all access modifiers as public for efficient data retrieval.
public String EMPNO

{

get

{

return _empno;

}

set

{

_empno
value;

}

}


 
Is this answer useful? Yes | No
December 14, 2007 06:24:43   #2  
ranjik2008 Member Since: December 2007   Contribution: 3    

RE: Is it possible to have different access modifiers on the get/set methods of a property? Justify
Yes It is possible to assign different access modifiers but the efficient approach is make all modifiers to public for accessibility
public
String EMPNO

{

get

{

return _empno;

}

set

{

_empno
value;

}

}


 
Is this answer useful? Yes | No
December 16, 2007 10:20:54   #3  
factpandit Member Since: December 2007   Contribution: 2    

RE: Is it possible to have different access modifiers on the get/set methods of a property? Justify

I think the right answer is No if I understand the question correctly.

We don't specify any access modifiers on "get" and "set" individually rather we specify on the wrapping property name.

in other words following is valid:

private int m_myInt -1;
public int MyProperty
{
get {return m_myInt;}


set {m_myInt value;}
}


However following is not valid

private int m_myInt -1;
public int MyProperty
{
public get{return m_myInt;}


private set{m_myInt value;}
}




 
Is this answer useful? Yes | No
December 26, 2007 01:47:09   #4  
amit.stefen Member Since: December 2007   Contribution: 1    

RE: Is it possible to have different access modifiers on the get/set methods of a property? Justify
factpandit is absolutely correct
 
Is this answer useful? Yes | No
January 21, 2008 03:48:38   #5  
chaos_hooi Member Since: January 2008   Contribution: 1    

RE: Is it possible to have different access modifiers on the get/set methods of a property? Justify
Yes and No depending on which version of .NET FW (or C#) that we are talking about.New feature in C#
 
Is this answer useful? Yes | No
February 06, 2008 10:53:36   #6  
musclebai Member Since: January 2006   Contribution: 13    

RE: Is it possible to have different access modifiers on the get/set methods of a property? Justify

You can have different access modifiers applied to the get and set methods.

public class Myclass
{
private string _MyProperty;
public string MyProperty
{
get { return _MyProperty; }
internal set { _MyProperty value; }
}
}

Thus types in the same assembly as MyClass have access to the set method.However the get method is public.This situation is very common in day to day applications where only a specified set of classes can set the value while most of the classes can read the values.

 
Is this answer useful? Yes | No
May 27, 2008 06:12:33   #7  
nikhilrawat23 Member Since: May 2008   Contribution: 15    

RE: Is it possible to have different access modifiers on the get/set methods of a property? Justify
no we cant give access modifier in the get set method........
we can set the access modifier of property only..............

 
Is this answer useful? Yes | No
July 02, 2008 12:11:35   #8  
John Jiang Member Since: June 2008   Contribution: 8    

RE: Is it possible to have different access modifiers on the get/set methods of a property? Justify
No. the only way is to create another propperty with different modifier. so one with get and another one with set
 
Is this answer useful? Yes | No
July 16, 2008 17:29:16   #9  
dragonsrightwing Member Since: July 2008   Contribution: 1    

RE: Is it possible to have different access modifiers on the get/set methods of a property? Justify
It is very easy if you are using .NET Framework 2.0 or above. See http://msdn.microsoft.com/en-us/library/75e8y5dd(VS.80).aspx

In short the restricion is always to a more restrictive level and only one accessor can be modified - for instance:
public int MyInt
{
get
{ return m_MyInt; }
private set
{ m_MyInt value; }
}

will compile but:

private int MyInt
{
get
{ return m_MyInt; }
public set
{ m_MyInt value; }
}
will not compile nor will:

public int MyInt
{
protected get
{ return m_MyInt; }
private set
{ m_MyInt value; }
}

 
Is this answer useful? Yes | No
August 18, 2008 23:34:41   #10  
amasood Member Since: August 2008   Contribution: 2    

RE: Is it possible to have different access modifiers on the get/set methods of a property? Justify
Yes : for set method i.e protected and

No:for get method

e.g
public class A
{
private string name;

public string Name
{
get
{
return name;
}
protected set
{
name value;
}
}
}

 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape