Car
Car
class or datatype in a program. These are mainly used to restrict unwanted data
manipulation by external programs or classes. There are 4 access modifiers
(public, protected, internal, private) which defines the 6 accessibility levels as
follows:
public,protected, internal, protected internal, private, private protected
Private
If you declare a field with a private access modifier, it can only be accessed
within the same class:
Access is only granted to the containing class. Any other class inside th
e current or another assembly is not granted access to these members
working!!
using System;
class Car
{
private string model = "Mustang";
Fail!!
using System;
class Car
{
private string model = "Mustang";
}
class Program
{
static void Main(string[] args)
{
Car myObj = new Car();
Public Modifier
If you declare a field with a public access modifier, it is accessible for all
classes:
Access is granted to the entire program. This means that another method or another
assembly
which contains the class reference can access these members or types. This access
modifier has
the most permissive access level in
comparison to all other access modifiers.
using System;
class Car
{
public string model = "Mustang";
}
class Program
{
static void Main(string[] args)
{
Car myObj = new Car();
class Car
{
public string model = "Mustang";
public int number = 1;
}
class Program
{
static void Main(string[] args)
{
Car myObj = new Car();
Car myObj2 = new Car();