CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2007
    Posts
    4

    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

  2. #2
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    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.

  3. #3
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  4. #4
    Join Date
    Jul 2007
    Posts
    4

    Re: About Main() and some basics

    Quote Originally Posted by MadHatter
    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?

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    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.

    Code:
    int i = null;                   // illegal
    int? j = null;                 //legal
    Nullable<int> k = null; // legal
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  6. #6
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    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.


    Code:
    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/lib...wc(VS.80).aspx
    Last edited by MadHatter; July 26th, 2007 at 10:45 AM.

  7. #7
    Join Date
    May 2007
    Posts
    1,546

    Re: About Main() and some basics

    Quote Originally Posted by MadHatter
    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.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  8. #8
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    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.

  9. #9
    Join Date
    May 2007
    Posts
    1,546

    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:

    Code:
    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.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured