Click here to Skip to main content
15,745,298 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)

String st = "Hello";

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

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.

Updated 20-Oct-11 3:09am
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
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

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.
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...
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

Share this answer
Sergey Alexandrovich Kryukov 20-Oct-11 21:19pm    
Correct, a 5.
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
Sergey Alexandrovich Kryukov 20-Oct-11 21:18pm    
Correct, a 5.
Sergey Alexandrovich Kryukov 20-Oct-11 21:20pm    
As other said, there is no default type.
If you are talking about string class then it is public in C#, or c++

Refer this[^]

and as already stated st is string type
Share this answer
the headless nick 20-Oct-11 8:40am    
Thanks for your answer :)
Anuja Pawar Indore 20-Oct-11 8:41am    
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.[^][^]
Share this answer
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;


see the topic
Class and Struct Accessibility

Default access specifier is internal
Best of luck.........
Share this answer
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".
Sergey Alexandrovich Kryukov 20-Oct-11 21:17pm    
However, this is valid for type declaration -- default is internal.

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