To Jost .NWDP Site

C#-1

Review Questions

  1. List some important windows used in the VS IDE.

  2. What are some common .Net file extensions?

  3. If a window is not visible that you need in VS, how do you open it?

  4. Give four ways of starting a .Net application.

  5. Give seven ways of stopping a .Net application.

  6. What is the parent class of every primitive datatype in .Net?

  7. What is the size of these datatypes?   int   double   char   bool

  8. What is a partial class?

  9. Why is this statement okay
    instead of
    but
    is not okay instead of
  10. What is round trip engineering for class diagrams?

  11. How is the Hebrew letter א (aleph, hex unicode code 05D0) represented as a character constant in C#?

  12. How is a culture-specific resource file created?
    Look at the Intro.Localization Example.

  13. How is an event handler method connected to the event that it handles in a WinForms project? in an ASP.Net project?
 

Examples

IntDataType  FormatStrings  Bitwise  ToBase64  CreateResX  WeekDaysEnum  Array  Structure  RepeatSentence  Square  MathMethods  CommandLineArgs
 
Example files: cs1.zip and cs1.txt.

 

C# Language Features

 

  1. Classes   Intro.Greeter1 Example

  2. Changing the Name of a Class

  3. Namespaces.

  4. The using Statement

  5. Primitive Constants.

  6. Primitive DataTypes   IntDataType Example

  7. Variables

  8. Format Strings   FormatStrings and Intro.CurrentDate Examples

  9. Operators   Bitwise Example

  10. Type Conversion Functions   ToBase64, CreateResX Examples

  11. Enum Declarations   WeekDaysEnum Example

    • Enums are usually used instead of defined constants in C#.

  12. Flow of Control

  13. One-Dimensional Arrays   Array Example

    • Declaring an array with four elements, all initialized to 0:
      int[] a = new int[4];

    • Arrays always have 0 as an index a lower bound.

    • Assigning a value to an array element:
      a[i] = v;

    • Some initialization styles for a 1-d array:
      int[] a = new int[4];
      int[] a = {1, 3, 5, 7};
      int[] a = new int[] {1, 3, 5, 7};
      int[] a = new int[4] {1, 3, 5, 7};

    • Resizing an existing array:
      a = new int[4];
      a = new int[] {1, 3, 5, 7};
      a = new int[4] {1, 3, 5, 7};

    • Obtaining the size of a 1-d array
      size = a.GetUpperBound(0) + 1;
      size = a.GetLength(0);
      size = a.Length;

    • Printing an array with a traditional for loop
      for(int i = 0; i <= a.GetUpperBound(0); i++)
          Console.WriteLine(a[i]);

    • Printing an array with a foreach loop
      foreach(int x in a)
          Console.WriteLine(x);

    • The Array class contains the static utility methods BinarySearch and Sort.

  14. Two-Dimensional Arrays

    • Declaring an two-d array with 3 rows and 4 columns:
      int[,] a = new a[3, 4];
      This creates an array with these elements:
      a[0, 0], a[0, 1], a[0, 2], a[0, 3],
      a[1, 0], a[1, 1], a[1, 2], a[1, 3],
      a[2, 0], a[2, 1], a[2, 2], a[2, 3]

    • Two-d Arrays C# have 0 as a lower bound in each dimension.

    • Assigning a value to an array element:
      a[i,j] = v;

    • Two-Dimensional Array Initialization Styles
      int[,] a = new int[3, 4];
      int[,] a = new int[,] {{1, 2, 3, 4}, {2, 3, 4, 5},
          {3, 4, 5, 6}};
      int[,] a = new int[3, 4] {{1, 2, 3, 4}, {2, 3, 4, 5},
          {3, 4, 5, 6}};

    • Obtaining the Size of a Two-Dimensional Array
      height = a.GetUpperBound(0) + 1
      height = a.GetLength(0)

      width = a.GetUpperBound(1) + 1
      width = a.GetLength(1)

      totalSize = a.Length

    • Printing a 2-d array with a traditional for loop
      for(int i = 0; i <= a.GetUpperBound(0); i++)
      {
          for(int j = 0; j <= a.GetUpperBound(1); j++)
             Console.WriteLine(a[i,j]);
          Console.WriteLine( );
      }

  15. Ragged Arrays

    • A ragged array is an array of one-d arrays:
      int[][] a = new int[3][];
      a[0] = new int[] {1, 2, 3};
      a[1] = new int[] {4};
      a[2] = new int[] {5, 6};

    • Printing a ragged array with nested for loops:
      for(int i = 0; i <= a.GetUpperBound(0); i++)
      {
          for(int j = 0; j <= a[i].GetUpperBound(0); j++)
             Console.Write(a[i][j] + " " );
          Console.WriteLine( );
      }

    • Printing a ragged array with nested foreach loops:
      foreach(int[] b in a)
      {
          for(int s in b)
             Console.Write(s + " " );
          Console.WriteLine( );
      }

  16. Scope

    • The scope of a variable consists of the sections of code where that variable can be used.

    • A global variable is declared outside of any method and can be used anywhere in the module or class where it is declared.

    • Global variables can be declared with private, public or internal accessibility.

    • A local variable is declared inside a method and can be used only in the procedure where it is declared.
      public class Class1
      {
          private int a;
          public static void Main( )
          {
             int b = 5;
             Console.WriteLine(a + " " + b);
          }
      }

    • A local variable declared in a block can only be used in that block.

    • A variable can be declared in a for statement:
      for (int i = 1; i <= 10; i++)
      {
          int x;
          ...
      }

  17. Structures   Structure Example

    • Computer memory is traditionally partitioned into two disjoint areas called the stack and the heap.

    • The location of memory allocated to a specific variable is called the address of the variable.

    • A value type has its memory entirely allocated on the stack.

    • A structure is a user defined value type.

    • Use the C# keyword struct to define a structure.

    • A reference type has its memory allocated on the heap and is accessed using a reference variable, which may have its address either on the stack or the heap.

    • A structure declaration:
      public struct person
      {
          public string name;
          public int id;
          public char gender;
          public short age;
      }

    • A structure can have constructors and methods.

    • A constructor for a structure type cannot be noarg (have an empty parameter signature). The noarg constructor is reserved for the default constructor supplied by C#.

    • Structures cannot participate in inheritance.

    • Structure variable use:
      Person x;
      x.name = "Sally"
      x.ID = 3453
      x.gender = "F"
      x.age = 11
      Console.WriteLine("{0} {1} {2} {3}", x.name, x.ID,
          x.gender, x.age)

    • Attributes from the InteropServices namespace can be used to precisely control the structure layout:
      using System.Runtime.InteropServices;
      ...
      [StructLayout(LayoutKind.Explicit)]
      public struct Person
      {
          [FieldOffset(0)]public string name;
          [FieldOffset(4)]public int id;
          [FieldOffset(8)]public char gender;
          [FieldOffset(12)]public short age;
      }

  18. Methods   RepeatSentence and Square Examples

    • An example method definition with return type int:

      public static int F(int a, int b)
      {
          ...
      }

    • A method with return type void does not return a value:

      public static void F(int a, int b)
      {
          ...
          return expression;...
      }

    • To call a void method, place the method with its parameters in parentheses on a line by itself:
      F("apple", 5, 3.26);

    • To call a method with a return value, place the function name with its parameters in parentheses in an expression:
      y = F(25) + 74;

    • Overloaded subroutines are permitted in C#. Two subroutines are overloaded if they have the same name but different signatures.

    • Special keywords for argument passing:

        ref Pass by reference. Assigning a value to parameter is optional.
        out Output parameter. Assigning a value to parameter is required.
        params Parameter array. See following points.

    • The keywords ref and out are used at invocation as well as in the method header.

    • Use parameter arrays when the number of parameters is not known until runtime:
      public static int sum(params int[] list)
      {
          int total = 0;
          foreach(int x in list)
             total += x;
          return total;
      }

    • The rules for parameter arrays:

        The parameter array must always be defined last in the method definition.

        The parameter array must be passed by value.

        The parameter array parameters are automatically optional. If none are specified, the parameter array is an array of length zero.

    • Methods of a class can be called by name. The name of the method is passed to the CallByName subroutine as a string.
      Interaction.CallByName(objectReference, methodName,
          callType, parameterArray);

    • The Interaction class is defined in the Microsoft.VisualBasic namespace.

  19. Methods in System.Math   Math Example

    • Utility Functions: Abs, Ceiling, Floor, Max, Min, Sign, Sqrt, Round

    • Logs and Exponents: Exp, Log, Log10, Pow

    • Trig Functions: ACos, ASin, ATan, ATan2, Cos, Sin, Tan

    • Hyperbolic Functions: Cosh, Sinh, Tanh

    • Math Constants: PI, E

  20. Command Line Arguments   CommandLine Example

    • Two ways to specify command line arguments in a console application:

      • Enter them in a Command Prompt (DOS) Window,

      • Enter them from the IDE: Select My Project from the Solution Explorer >> Debug Tag. Set the command line arguments in the provided textbox.

    • To obtain the command line arguments of a running application, declare the Main method as
      public static void Main(string[] args)
      {
          ...
      }