C # programming language-__ algorithm for Java Developers

Source: Internet
Author: User
Tags naming convention
The C # programming language for Java developers. The C# language introduces new language constructs, such as foreach, indexers, properties, delegates, operator overloads, and other language constructs. We'll discuss these constructs in more detail later in this article. This page content source convention top level declaration fully qualified name and namespace alias preprocessing Directive language syntax
Source File Conventions
  
We need to know that the two languages differ in the file naming conventions and structures of the source program:
  
File naming
The naming convention for files that contain C # classes is somewhat different from Java. First, in Java, the extension of all source files is. java. Each source file contains a top-level public class declaration, and the class name must match the file name. In other words, a class named Customer with a public scope declaration must be defined in a source file that has a name Customer.java.
  
The C # source code is represented by the. cs extension. Unlike Java, a source file can contain multiple top-level public class declarations, and file names do not need to match any class names.
  
   Top Level Declaration
  
In Java and C #, source code starts with a top-level declaration in a certain order. declarations in Java and C # programs only have a slight difference.
  
Top-level declarations in Java
In Java, we can group classes together with the package keyword. The packaged class must use the Package keyword in the first executable row of the source file. Then there is any import statement that requires access to the classes in the other package, followed by the class declaration, such as:
  
package;
Import.;
Class Customer
{
...
}
  
Top-level declarations in C #
C # uses the concept of namespaces to group logically related classes with the namespace keyword. These practices are similar to Java packages, and classes with the same name can appear in two different namespaces. To access a class that is defined in a namespace other than the current namespace, we can use a using keyword that immediately follows the namespace name, as follows:
  
Using.;
Namespace
{
Class Customer
{
...
}
}
  
Note that a using statement can be completely legitimately placed in a namespace declaration, in which case the imported namespace forms part of the containing namespace.
  
Java does not allow multiple packages in the same source file, and C # allows multiple namespaces in a. cs file:
  
Namespace Acmeaccounting
{
public class Getdetails
{
...
}
}
Namespace Acmefinance
{
public class ShowDetails
{
...
}
}
  
   fully qualified name and namespace alias
  
Like Java, by providing a fully qualified name for a class (such as System.Data.DataSet or the acmeaccounting.getdetails in the above example), we can access. NET without a using reference to a namespace. Or a class in a user-defined namespace.
  
The fully qualified name may become long and inconvenient, in which case we can use the Using keyword to specify a short or alias to improve the readability of the code.
  
In the following code, an alias is created to refer to code written by a fictitious company:
  
Using Datatier = Acme.SQLCode.Client;
Using System;
public class Outputsales
{
public static void Main ()
{
int sales = Datatier.getsales (January);
Console.WriteLine (Januarys Sales: {0}, sales);
}
}
  
Note The syntax for the WriteLine (), with the {x} in the format string, where x represents the position of the parameter list for the value to be inserted here. Assuming the Getsales () method returns 500, the output of the application will be:
  
Januarys sales:500
  
   preprocessing Directives
  
Similar to C and C + +, C # includes preprocessor directives, which provide the ability to conditionally skip parts of a source file, report errors and warning conditions, and describe different parts of the source code. The term "preprocessing directives" is used only to maintain consistency with the C and C + + programming languages, because C # does not include separate preprocessing steps. For a complete list of C # preprocessor directives, see C # Preprocessor directives.
  
   Language Grammar
  
In this section, we discuss similarities and differences between the two languages. Some of the major differences are:
  
The constant declaration-java uses the final keyword for this purpose, and C # uses the keyword const or readonly.
  
Composite data type-in Java, we can use the Class keyword to create a composite data type as a class without a method, but C # provides struct for this, as in C.
  
The destructor-c# allows us to create a destructor method that is called before an instance of the class is destroyed. In Java, you can provide a finalize () method to contain the code that clears the resource before the object is garbage collected. In C #, this functionality is provided by the class destructor. A destructor resembles a constructor that has no parameters and preceded with the tilde "~".
  
function pointer-c# provides a construct called delegate to create type-safe function pointers. Java does not have any mechanism equivalent to it.
  
Data type
C # provides all the data types available in Java, and adds support for unsigned numbers and new 128-bit high-precision floating-point types.
  
In Java, for each basic data type, the core class library provides a wrapper class to represent it as a Java object. For example, an Integer class wraps an int data type, and a double class wraps a double data type.
  
In C #, all basic data types are objects in the System namespace. For each data type, a short name or alias is provided. For example, int is the abbreviation for System.Int32, and double is the abbreviated form of System.Double.
  
The following list shows the C # data types and their aliases. As you can see, the first 8 correspond to the basic types available in Java. Note, however, that a Boolean in Java is called bool in C #.
   

Because C # represents all the basic data types as objects, it is possible to invoke the object method according to the base data type. For example:
  
int i=10;
Console.WriteLine (i.ToString ());
  
This can be achieved with the help of automatic boxing and unboxing. For more information, see Boxing and unboxing.
  
Enumeration
Similar to C + +, enumerations can be used in C # to combine named constants, and enumerations cannot be used in Java. The following example defines a simple Color enumeration.
  
public enum Color {Green, Orange, Red, Blue}
  
You can also assign an integer value to an enumeration, as shown in the following enumeration declaration:
  
public enum Color {green=10, orange=20, red=30, blue=40}
  
The following program calls the GetNames method of the enum type to display the available constants for the enumeration. It then assigns the value to the enumeration and displays the value.
  
Using System;
  
public class TypeTest
{
public static void Main ()
{
Console.WriteLine (Possible Color choices:);
Enum.getnames returns a string array of named constants for the enum
foreach (String s in Enum.getnames (typeof (Color))
{
Console.WriteLine (s);
}
Color FavoriteColor = Color.Blue;
Console.WriteLine (Favorite Color is {0},favoritecolor);
Console.WriteLine (favorite Color value is {0}, (int) favoritecolor);
}
}
  
After running, the program will display the following results:
  
Possible Color choices:
Green
Orange
Red
Blue
Favorite Color is Blue
Favorite Color value is 40
  
String
In Java and C #, string types exhibit similar behavior, with only a few subtle differences. Both of the string types are immutable, which means that once the string is created, the value of the string cannot be changed. In both instances, the method that looks like the actual content of the string actually creates a new string for return, leaving the original string unchanged. In C # and Java, the process of comparing string values is different. In Java, in order to compare the value of a string, developers need to call the Equals () method according to the string type, just as by default = = operator to compare reference types. In C #, developers can use the = = or!= operators to directly compare the values of strings. In C #, although strings are reference types, by default, the = = and!= operators compare the values of strings rather than references. Later in this article, we'll discuss value types and references.
  
As in Java, C # developers should not use string types to concatenate strings to avoid the overhead of creating new string classes each time the string is concatenated. Instead, developers can use the StringBuilder class in the System.Text namespace, which is functionally equivalent to the StringBuffer class in Java.
  
String
C # provides the ability to avoid the use of escape sequences in string constants, such as "" or "" for backslash characters, that represent tabs. To do this, you can declare a string by using the @ symbol before assigning a value to the string. The following example shows how to use an escape character and how to assign a value to a string:
  
Using escaped characters
string path =//fileshare/directory/file.txt;
  
Using String Literals
string escapedpath = @/filesharedirectoryfile.txt;
  
   Conversions and casts
  
Java and C # conform to similar data type automatic conversions and cast rules.
  
Like Java, C # supports both implicit type conversions and explicit type conversions. In the case of widening conversions, conversions are implicit. For example, the following conversions from int to long are implicit, as in Java:
  
int intvariable = 5;
Long L = intvariable;
  
The following is a list of implicit conversions between. NET data types:
   

You can use the same syntax as Java to cast an expression that you want to explicitly convert:
  
Long longvariable = 5483;
int intvariable = (int)
Related Article

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.