In the this program if you write public in front of
static void Display(Employee num) program is not executing. Please tell me the reason. Problem is highlighted.
But internal access modifier instead of public in the program is executing.
using System;
public class CreateEmployee
{
public static void Main()
{
Employee myAssistant = new Employee();
myAssistant.SetId(345);
Display(myAssistant);
Console.ReadKey();
}
static void Display(Employee num)
{
Console.WriteLine("ID # is {0}", num.GetId());
}
}
internal class Employee
{
private int idNumber;
public int GetId()
{
return idNumber;
}
public void SetId(int id)
{
idNumber = id;
}
}
// ID # is 345
Loading
Posted Jul 13, 2013, 10:37 AM
VulpesPosted Jul 13, 2013, 7:21 AM
Inside a public method any public, internal or protected internal type or member within the same assembly can be accessed. So can any private or protected member (including inherited protected members) within the same type.
Posted Jul 13, 2013, 7:03 AM
VulpesPosted Jul 13, 2013, 6:48 AM
'internal' means accessible within the type itself or within the current assembly.
Posted Jul 13, 2013, 6:31 AM
VulpesPosted Jul 13, 2013, 4:06 AM
Iftikar HussainPosted Jul 13, 2013, 2:34 AM
The reason is - Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly. In other words, any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined. You can refer below link for more details.
http://msdn.microsoft.com/en-us/library/ba0a1yw2(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/ms173121.aspx
Regards,
Iftikar