About Main() and some basics

HI,
I need the exact answers for these cz I am confused.

1)is Main() in c# should be public static or only static?
2)by default(if we doesnt specify one) what is the what will be the
access modifier in c# for a method,property,attribute,class/
interface/,assembly.
3)What are the "attributes" that specify an object's state?
4)what is string in c#(both string keyword and String Class) I mean
whether it is value type or Reference type?
5)With the introduction of nullable types in 2.0 what is the answer
for "null can be stored in value types"?
6)"All local variables must be intialized before using" whether they
are Value type variables or Refernce Type?

These questions looks bit funny but please help me getting a clear
idea of them.

Thanks in advance
[836 byte] By [sribha] at [2007-11-20 9:36:41]
# 1 Re: About Main() and some basics
1. needs to be static access modifier is irrelevant.
2. depends on what you're defining. classes are internal, members are private in their default state.
3. what?
4. string, int, float, bool, and the others are aliases for the System.String, System.Int32, System.Single, System.Boolean and so on...
5. yea if its a nullable type
6. value types do not have to be initialized w/ new in order to use them. if they contain reference types those will be null and will need to be initialized before use.
MadHatter at 2007-11-9 11:34:28 >
# 2 Re: About Main() and some basics
4)what is string in c#(both string keyword and String Class) I mean
whether it is value type or Reference type?

As MH said, string is an alias for System.String. This is a non-mutable (i.e. read only) reference type. I.e. none of its methods change its data. When you want to change a string you need to create a new one with the new value.

Darwen.
darwen at 2007-11-9 11:35:28 >
# 3 Re: About Main() and some basics
1. needs to be static access modifier is irrelevant.

6. value types do not have to be initialized w/ new in order to use them. if they contain reference types those will be null and will need to be initialized before use.

If I declare
static void Main()
{
int i;
//and try to print this
System.Console.WriteLine(i.ToString());
//it raises an compile time error
}

so here i is a value type or refernece type?
sribha at 2007-11-9 11:36:28 >
# 4 Re: About Main() and some basics
Everything has to be initialised before use, be it reference or not. Also, you can't store a null in a value type (just to make it clear). However you can store a null in a Nullable value type.

int i = null; // illegal
int? j = null; //legal
Nullable<int> k = null; // legal
Mutant_Fruit at 2007-11-9 11:37:34 >
# 5 Re: About Main() and some basics
it probably does not check whether its a value type or reference type for local variables and therefore generates the error (notice the verbage "possibly" in the error)

you do not have to initialize value types in order to use them. try the following code.

value types cannot be null and therefore have default values. when you have a default value you do not need to initialize it before you use it, therefore that statement is not true.

public class Test {
static SomeStruct ss;
static viod Main() {
Console.WriteLine(ss.I.ToString());
}
}
public struct SomeStruct {
public int I;
}

just in case you wanted to ask a question about default values in a job interview, here's the default value table for value types (default value for reference types is null): http://msdn2.microsoft.com/en-us/library/83fhsxwc(VS.80).aspx
MadHatter at 2007-11-9 11:38:33 >
# 6 Re: About Main() and some basics
you do not have to initialize value types in order to use them. the complier is going to check and make you initialize it before you use it if its declared in local scope, but the following code and then tell me that Everything has to be initialized before use.
If that was a reference type it'd be initialised to null aswell. You don't *have* to initialise reference types declared as member variables either.

The reason is that (i believe) all member variables are automatically set to default(T) when your class is instantiated. For reference types, this means setting to null. For value types, this means zeroing the memory. Unfortunately this can leave value types in an 'invalid' state.
Mutant_Fruit at 2007-11-9 11:39:33 >
# 7 Re: About Main() and some basics
value types do not need to be initialized before you use them. reference types do not need to be initialized either (but then you can't use them), but the difference is that your value type will have a value, your reference type will throw a runtime exception.

default value has to do with where the data is stored. since reference types are stored in the managed heap they are simply a pointer, where as value types are a non-pointer object and are given default values.
MadHatter at 2007-11-9 11:40:38 >
# 8 Re: About Main() and some basics
Ah, yeah, sorry. I get your point.

Well, thing is you can't always safely use a struct which contains an enum. Suppose i have this enum:

enum MyEnum
{
A = 1,
B = 2,
C = 3
}

The default value of a struct containing this enum will set the value to be '0' which is not actually a valid enum. This is why i was saying you 'have' to initialise a struct/value type before using it. But yes, what i said as technically incorrect. You can actually use a value type without initialising it in certain circumstances.
Mutant_Fruit at 2007-11-9 11:41:31 >