C# Basic (2)

1.

int foo = 42;         // Value type.
object bar = foo;     // foo is boxed to bar.
int foo2 = (int)bar;  // Unboxed back to int.

Notice that when boxing;that is, once again, when converting from a value type to a reference type—there is no explicit cast needed. However, when unboxing—converting from a reference type to a value type—the cast is needed. This is because in the case of unboxing, an object could be cast to any type.

 

2.

class Employee { }

class ContractEmployee : Employee { }

class CastExample1
{
    public static void Main ()
    {
        Employee e = new ContractEmployee();
    }
}

ContractEmployee ce = new Employee(); // Won’t compile.Remember that the rules of substitutability state that a derived class can be used in place of its base class. Therefore, an object of type ContractEmployee should always be able to be used in place of or as an Employee object. That’s why the first example compiles.However, you cannot cast an object of type Employee down to an object of type ContractEmployee because there’s no guarantee that the object supports the interface defined by the ContractEmployee class. Therefore, in the case of a downcast, an explicit cast is used as follows:

//Downcast will fail.

ContractEmployee ce = (ContractEmployee)new Employee();

class Employee { }

class ContractEmployee : Employee { }

class CastExample4
{
    public static void Main ()
    {
        Employee e = new Employee();
        ContractEmployee c = (ContractEmployee)e;
    }
}

The program compiles, but running the program generates a run-time exception. There are two things to note here. First, the result is not a compile-time error because e might have really been an upcasted ContractEmployee object. Therefore, the true nature of the upcasted object can’t be known until run time. Second, the CLR determines the object types at run time. When the CLR recognizes an invalid cast, it throws a System.InvalidCastException.

 

3.

Access Modifier
Description
public
Signifies that the member is accessible from outside the class’s definition and hierarchy of derived classes.
protected
The member is not visible outside the class and can be accessed by derived classes only.
private
The member cannot be accessed outside the scope of the defining class. Therefore, not even derived classes have access to these members.
internal
The member is visible only within the current compilation unit. The internal access modifier creates a hybrid of public and protected accessibility depending on where the code resides.

For every member, it should has the modifiers before it, else the default one is private.

 

4.  static public void Main()

using System;
class CommandLineApp
{
    public static void Main(string[] args)
    {
        foreach (string arg in args)
        {
            Console.WriteLine("Argument: {0}", arg);
        }
    }
}

And here’s an example of calling this application with a couple of randomly selected values:

e:>CommandLineApp 5 42
Argument: 5
Argument: 42

the array of command-line arguments in C# does not contain the application name as the first entry in the array.

 

5. Multiple Main Methods

using System;

class Main1
{
    public static void Main()
    {
        Console.WriteLine("Main1");
    }
}

class Main2
{
    public static void Main()
    {
        Console.WriteLine("Main2");
    }
}

To compile this application such that the Main1.Main method is used as the application’s entry point, you’d use this switch:

csc MultipleMain.cs /main:Main1

Changing the switch to /main:Main2 would then use the Main2.Main method. C# is case-sensitive, you must be careful to use the correct case of the class in the switch as well.

 

6.  You can create objects in C# only by using the new keyword.

Reference types are created on the heap and value types are created on the stack. Therefore, the new keyword lets you create a new instance of a class, but it doesn’t determine where that object is created.

Why would you declare an object without instantiating it? Declaring objects before using them—or ” new-ing” them—is done in cases in which you’re declaring one class inside of another class. This nesting of classes is called containment, or aggregation.

By default, each member is defined as an instance member, which means that a copy of that member is made for every instance of the class. When a member is declared as a static member, there is only one copy of the member. A static member is created when the application containing the class is loaded, and it exists for the life of the application.

using System;

class InstCount
{
    public InstCount()
    {
    instanceCount++;
    }
     static public int instanceCount = 0;
}

class AppClass
{
    public static void Main()
    {
        Console.WriteLine(InstCount.instanceCount);

        InstCount ic1 = new InstCount();
        Console.WriteLine(InstCount.instanceCount);

        InstCount ic2 = new InstCount();
        Console.WriteLine(InstCount.instanceCount);
    }
}

In this example, the output would be as follows:

0
1
2

 

7.

use A: A();

using System;

class A
{
    public A()
  {
        Console.WriteLine("A");
    }

    public A(int foo)
    {
        Console.WriteLine("A = {0}", foo);
    }
}

class B : A
{
    public B(int foo)
  {
        Console.WriteLine("B = {0}", foo);
    }
}

class DerivedInitializer1App
{
    public static void Main()
    {
        B b = new B(42);
    }
}

use A(int foo);

using System;

class A
{
    public A()
    {
        Console.WriteLine("A");
    }

    public A(int foo)
    {
        Console.WriteLine("A = {0}", foo);
    }
}

class B : A
{
    public B(int foo) : base(foo)
    {
        Console.WriteLine("B = {0}", foo);
    }
}

class DerivedInitializer2App
{
    public static void Main()
    {
        B b = new B(42);
    }
}

Unlike in Visual C++, you cannot use constructor initializers to access instance members, aside from constructors, of the current class.

 

8.

There are only two rules to keep in mind when defining something as a const. First, a constant is a member whose value is set at compile time, either by the programmer or defaulted by the compiler. Second, a constant member’s value must be written as a literal.

There’s no need for the client to instantiate the MagicNumbers class because by default const members are static.

 

 

Advertisements
This entry was posted in C#. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s