Basic exercises and Answers for C # Network application programming (II.)

Source: Internet
Author: User
Tags foreach format array exception handling garbage collection implement include variables
Programming | network

  1. What are the types of data that C # supports? What are the features compared to C + +?

Troubleshooting

The data types supported by C # are:

(1) Value type

Includes: Simple type, struct type, enum type. Among them, the simple type is divided into: integral type, Boolean type, character type, floating point type, decimal type.

(2) Reference type

Include: Object type, class type, interface, metadata, string type, array.

The main features of C # compared to C + + are:

1 The C # language has evolved from C + +. However, it is designed exactly according to the object-oriented idea, and guarantees the security of the type.

2 C # simplifies the use of C + + in classes, namespaces, method overloads, and exception handling. The complexity of C + + has been discarded, making it easier to use and less error-prone.

3 C # Reduces some of the features of C + +, there are no more macros, multiple inheritance. For enterprise developers in particular, the above features will only create more trouble than benefits.

4 C # adopts strict type safety, version control, garbage collection (garbage collect) and so on. All of these features are aimed at developing component-oriented software development.

5 C # no longer has the "::", ".", and "->" operators, using only a single operator ".".

6 C # uses the unified type system, abandons the C + + changeable type system.

7 in C #, global functions, variables, or constants cannot be defined outside the class. All things must be encapsulated in a class, including instance members or static members. This makes C # code easier to read and helps reduce potential naming conflicts.

8 in C #, you cannot use variables that are not initialized. This avoids errors in the calculation resulting from the use of uninitialized variables.

  2. What is the difference between a value type and a reference type in the C # language?

Troubleshooting

The difference between a value type and a reference type is that a variable of a value type holds the actual data directly, whereas a variable of a reference type holds the address of the data, which is the reference of the object.

The value type variable directly stores the value of the variable in the stack, and the variable of the reference type saves the actual data address on the stack, while the actual data is stored in the heap. Note that the heap and stack are two different concepts, the storage locations in memory are also different, and the heap is typically used to store variable-length data, such as string types, while the stack is used to store fixed-length data, such as the integer type's data int (four bytes per int variable). The location of the data store is known to be that when a value variable is assigned to another value variable, holds two identical values in the stack, and a reference variable is assigned to another reference variable, the two references to the same heap location are saved on the stack, that is, the address of the same heap in the stack. In the case of data manipulation, for value types, because each variable has its own value, the operation on one variable does not affect the other; For a variable of a reference type, manipulating the data of a variable is the operation of the variable in the heap, if the two reference-type variables refer to the same object, The actual implication is that they have the same address as the heap stored in the stack, so the operation on one variable affects another variable that references the same object.

  3. What is the difference between structure and class?

Troubleshooting

1 The structure is a value type, stored on the stack, and the class is a reference type, stored on a controlled heap.

2 manipulate the data in the structure faster than the data in the class or object.

3 typically store multiple types of data in a structure, which is more efficient when creating a small object shared by many classes or objects.

  4. What are the characteristics of an array type in C #?

Troubleshooting

1 arrays are typically used to store data of the same type, including the type object.

2 The array is a reference type, not a value type.

3 C # In addition to a one-dimensional array, multidimensional array, there are staggered array.

  5. What is the principle of conversion between different integral types in C #?

Troubleshooting

When converting between integral types, a small range type can be implicitly converted to a large range type, but an explicit conversion is required when a large range type is converted to a small range type.

  6. Briefly describe the process of boxing and unpacking.

Troubleshooting

Boxing is the implicit conversion of a value type to type Object or to an interface type implemented by that value type. Boxing a number assigns an object instance to it and copies the value to the new object. A unboxing is an explicit conversion of an object type to a value type, or an interface type implemented by a value type to that value type.

  7. Which of the following is the wrong formulation?

1 if (nmyvalue1=5) I=1;

2 if (nmyvalue2==1) I=1;

3) int[] myint={1,2,3};

foreach (int test in myInt)

{

test++;

Console.WriteLine (temp);

}

4) int[] myint1={1,2,3};

foreach (int test in myInt1)

{

Console>writeline (test);

}

Troubleshooting

1) error. The conditional expression in the if result is not a Boolean.

2) correct.

3) Error One: TEMP is not defined.

Error two: In a foreach block, test is read-only as an enumeration member and cannot be modified with test++.

4) error. The console should be a dot, not a greater-than number.

  8. What is the difference between error and exception, why do exception handling, and what are the statements for exception handling?

Troubleshooting

An error is an event that occurs during code execution that interrupts or interferes with the normal flow of code and creates an exception object. When an error breaks the process, the program attempts to find an exception handler (a piece of code that tells the program how to respond to the error) to help the program recover the process. In other words, the error is an event, and the exception is the object created by the event.

When you use the phrase "generate an exception," An error occurs in the method that indicates the problem, and the exception object (the information that contains the error and the time and position it takes) is created to ring the error. Factors that cause errors and subsequent exceptions include user errors, resource failures, and programming logic failures. These errors are related to the way the code implements a particular task, regardless of the purpose of the task.

If you do not do exception handling, that is, no response to errors, the robustness of the program will be greatly compromised, and even can not guarantee normal operation, so you must do exception handling.

The statements used for exception handling are: Try-catch statements, try-catch-finally statements, throw statements.

  9. Write a console application to output the square value of 1 to 5, requiring:

1) implemented with a for statement.

2) with the while statement implementation.

3) implemented with Do-while statement.

Troubleshooting

Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace Outputsquarevalue
{
Class Program
{
static void Main ()
{
Implement with a For statement
for (int i = 1; I <= 5; i++)
{
Console.WriteLine ("{0} squared value is {1}", I, I * i);
}
Using the while statement to implement the
int j = 0;
while (j + + < 5)
{
Console.WriteLine ("{0} squared value is {1}", J, J * j);
}
Implement with Do-while statement
int k = 1;
Todo
{
Console.WriteLine ("{0} squared value is {1}", K, K * k);
while (k++ < 5);
Console.ReadLine ();
}
}
}

  10. Write a console application that asks the user to enter 5 uppercase letters, prompts for help information and asks to re-enter if the user enters information that does not meet the requirements.

Troubleshooting

Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace Inputcapitalletter
{
Class Program
{
static void Main ()
{
bool OK = false;
while (ok = false)
{
Console.Write ("Please enter 5 uppercase letters:");
String str = Console.ReadLine ();
if (str. Length!= 5)
{
Console.WriteLine ("You enter the number of characters is not 5, please re-enter.") ");
}
Else
{
OK = true;
for (int i = 0; i < 5; i++)
{
char C = str[i];
if (C < ' A ' | | | c > ' Z ')
{
Console.WriteLine ("{0} characters ' {1}" is not a capital letter, please re-enter it.) ", I + 1, c);
OK = false;
Break
}
}
}
}
}
}
}

  11. Write a console application that requires the following functions to be completed.

1 receives an integer n.

2 If you receive a positive value of N, the output of all integers between 1 and N.

3 If the value received is negative, use break or return to exit the program.

4) go to (1) continue to receive the next integer.

Troubleshooting

Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace Testoutput
{
Class Program
{
static void Main ()
{
while (true)
{
Console.Write ("Please enter an integer (negative end):");
String str = Console.ReadLine ();
Try
{
int i = Int32.Parse (str);
if (I < 0) break;
for (int j = 1; J <= I; j) Console.WriteLine (j);
}
Catch
{
Console.WriteLine ("You enter not a number or a range that is beyond the integer, please re-enter");
}
}
}
}
}

  12. Write a console application to find all the "numbers" within 1000. The term "finish" refers to the sum of all the factors in which a number is exactly equal to it. For example, 6 is the end of the number because of 6=1+2+3.

Troubleshooting

Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace Completenumber
{
Class Program
{
static void Main (string[] args)
{
for (int i = 2; I <= 1000; i++)
{
int s = 1;
String str = "1";
for (int j = 2; J <= (int) math.sqrt (i); j + +)
{
if (J * (i/j) = i)
{
if (J!= i/j)
{
S + + j + i/j;
str = string. Format ("+{0}+{1}", J, i/j);
}
Else
{
S + j;
str = string. Format ("+{0}", j);
}
}
}
if (s = = i) Console.WriteLine ("{0}={1}", I, str);
}
Console.ReadLine ();
}
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.