Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
3.86/5 (3 votes)
See more:
My Question is rather Simple but I just can't help my curiosity. Does anyone know what is the default type of a variable in C# (or more generally in OOPS)

C#
String st = "Hello";


What is the type (Access Modifiers) of this
String st 
here.

Please provide suitable refrences for your answer. I tried to google it but nothig is specificly given about the TYPE of a variable. Some say its public others say private.

Thanks...
Posted
Updated 20-Oct-11 3:09am
v4
Comments
André Kraak 20-Oct-11 8:31am    
Did you mean default value?
Because the type of st in both cases is String as you yourself have indicated by declaring them of the type String.
the headless nick 20-Oct-11 8:35am    
Access Modifiers I mean. Yes..

You are mixing up two different terms here.
1) Type
2) Access Modifier

Type of "String st;" is System.String.

Default access modifier of variable/object is private.
 
Share this answer
 
v2
Comments
the headless nick 20-Oct-11 8:38am    
I am sorry for the Confusion. well type is what we call "Access Modifiers' commonly. Don't we..?
RaisKazi 20-Oct-11 8:45am    
Happy you get the Answer. But Type is not the Access Modifier. Lets take an example.

public Class ABC
{
//something
}

public Class Main
{
void Method1()
{
ABC obj=new ABC();
}
}

In above code, Type of obj is ABC and it's default access modifier is private.

Hope this clarifies. :)
Sergey Alexandrovich Kryukov 20-Oct-11 21:19pm    
Correct, a 5.
--SA
public & private is not a type of variable..
it keywords to specify access levels for member variables. See This

And Your Answer is (access levels for member variables): Private

Let test this example...
C#
class Test
    {
        String Test1;
        public string Test2;
        protected string Test3;
        private string Test4;
    }

    class TestAccessLavel
    {
        static void Main()
        {
            Test e = new Test();
            // e.Test1 Not acessable
            String Test2 = e.Test2;
            //e.Test3 Not acessable
            //e.Test4 Not acessable
        }
    }


Create one class Test and create variable (see above)
Now in other class main() create object of Test and try to acess Variable..

If Test1 & Test2 is acessable - public
Test1 & Test3 is acessable - protected
Test1 & Test4 is acessable - private

Thanks
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Oct-11 21:19pm    
Correct, a 5.
--SA
In most cases the default access is private, including field declarations of the type you ask about.

The default access for a top level class, struct or interface, i.e. as in this example
namespace Test {
 class NoAccessModifier { }
}

... is internal. Note however that the default access for a nested class/struct/interface (i.e. one which is declared inside another class) is private, just like fields, methods etc defined inside a class).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Oct-11 21:18pm    
Correct, a 5.
--SA
Sergey Alexandrovich Kryukov 20-Oct-11 21:20pm    
As other said, there is no default type.
--SA
If you are talking about string class then it is public in C#, or c++

Refer this
http://msdn.microsoft.com/en-us/library/system.string(v=vs.71).aspx[^]

and as already stated st is string type
 
Share this answer
 
Comments
the headless nick 20-Oct-11 8:40am    
Thanks for your answer :)
Anuja Pawar Indore 20-Oct-11 8:41am    
Welcome
It is String only.
In the first case, you assigned "Hello" to it. While in second it is blank string "" by default. That's the only difference.

If you mean access specifier, then see the link.
http://msdn.microsoft.com/en-us/library/ba0a1yw2%28VS.80%29.aspx[^]
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/82a16a51-a6a6-41f0-89fb-66fd8876e06f[^]
 
Share this answer
 
v3
Comments
the headless nick 20-Oct-11 8:32am    
I didn't get you. Is it public or private..? or just String..?? :-O plz provide any refrence.
Prerak Patel 20-Oct-11 8:33am    
Added a link.
the headless nick 20-Oct-11 8:36am    
Yeah... seen. Well thanks... :))
string s;
is same as
internal string s;


Link:-http://msdn.microsoft.com/en-us/library/ms173121.aspx[^]

see the topic
Class and Struct Accessibility

Default access specifier is internal
Best of luck.........
 
Share this answer
 
Comments
BobJanova 20-Oct-11 11:14am    
No, default for members (which a string is) is private, not internal.
Sergey Alexandrovich Kryukov 20-Oct-11 21:18pm    
He mixed up type declaration and member declaration, see below. Voted "1".
--SA
Sergey Alexandrovich Kryukov 20-Oct-11 21:17pm    
Lie!
However, this is valid for type declaration -- default is internal.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900