1

I'm new to Programming! Ignore if it a silly question. But leave a comment.

Is it possible to declare an access specifier to object instances in C#? OR is there any default specifiers for that ?

class Person
{
    public int age;
}

class Program
{
    static void Square(Person a, Person b) // Here note down "a" and "b" are instances
    {
        a.age = a.age * a.age;
        b.age = b.age * b.age;
        Console.WriteLine(a.age+" "+b.age);
    }
}
Iridium
  • 23,323
  • 6
  • 52
  • 74
Balaji
  • 1,375
  • 1
  • 16
  • 30
  • 1
    Default specifier for class members is `private'. But what do you mean by specifier for instance? – Ali Sepehri.Kh Dec 17 '14 at 07:50
  • 1
    You have already declared one, `public`. See [Access Modifiers MSDN](http://msdn.microsoft.com/en-us/library/ms173121.aspx) – Sayse Dec 17 '14 at 07:51
  • 1
    Have a look at [MSDN description of C# access modifiers](http://msdn.microsoft.com/en-us/library/ms173121.aspx) – Andrey Korneyev Dec 17 '14 at 07:52
  • Have a look at the MSDN Access Modifiers - C# Programming Guide (http://msdn.microsoft.com/en-us/library/ms173121.aspx). Like in your source code without explicitly declaring it's "Internal is the default if no access modifier is specified." – SSchuette Dec 17 '14 at 07:53
  • The instances Person a and b are known only in the method you´re using them. Is this what you mean? However the caller of Square of course knows those objects as well – MakePeaceGreatAgain Dec 17 '14 at 07:53
  • Ok all are saying the same thing except @AliSepehri.. I am asking this for reference type. – Balaji Dec 17 '14 at 08:00
  • Access modifiers are only applicable to the types or type members. You can't restrict access to an object *instance*. What *exactly* you're want to do? – Dennis Dec 17 '14 at 08:23
  • I worked **Out** Keyword with types int and String, I'm trying can it be possible on objects Or Any reference type. @Dennis – Balaji Dec 17 '14 at 09:02
  • @Sayse No, its not a duplicate.. Not just Access specifiers. – Balaji Dec 17 '14 at 09:04
  • 1
    @Balaji: I'm trying to link your question with your comment. How access modifiers are related to `out` parameters? Yes, you can make `out` of any reference type, but what is that to access modifiers? – Dennis Dec 17 '14 at 09:38
  • Its not related to Access Specifier, but i suddenly got stuck with this thought, Whether it is possible or not @Dennis – Balaji Dec 17 '14 at 09:50

2 Answers2

2

The default for classes is internal, meaning, they can only be accessed by types in the same assembly. If a class is not contained inside other class it can also be public, in which case, it is accessible by any type. If it is declared inside other class, then it can also be protected, only accessible by the containing class or its subclasses, private, only accessible by the containing class, public, freely accessible or protected internal, meaning, accessible by the declaring class, its subclasses or types in the same assembly. The default for nested classes is private. A member (property, field or event) can also be private (default), public, protected, internal or protected internal.

Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74
1

When you instantiate from a class, you can define its access scope by modifiers (This modifier is for type member(reference)).

Modifiers are used to modify declarations of types and type members

Look at following examples:

1)

class Employee
{
    private Person person;  //private is modifier for person type member, not for Person class
}

2)

static void Square(Person a, Person b){...}   //The access scope for a and b is equal with method scope

For example this is not instance modifier:

public class Person
{...}
Ali Sepehri.Kh
  • 2,468
  • 2
  • 18
  • 27
  • 1
    "private is modifier for instance" - `private` is modifier for type member, not for the particular instance of `Person`. – Dennis Dec 17 '14 at 08:25
  • @Dennis: I changed my word, I wanted to use the word of questioner, but yeah that is better to say type member. Thank you :) – Ali Sepehri.Kh Dec 17 '14 at 08:34