View on GitHub

Bearded-android-docs

MigratingFromJava1

Download this project as a .zip file Download this project as a tar.gz file

Created Tuesday 07 January 2014

Similarities

Differences

Java Keywords With No C# Equivalents

C# Keywords With No Java Equivalents

Object o = //...
string s = o as string;
if (s != null) //...

checked
{
   try
   {
        short x = 32767;
        short y = 32767;
        short z = y + z;
   }
   catch (OverflowException e) { //... }
}

object o = 10;
dynamic d = 10;
int x = (int) o + 3; //cast needed
int y = d + 3;// cast not needed!

class Apartment
{
    public string name;

    //convert an aparment to a house
    public static explicit operator House(Apartment a)
    {
	return new House(a.name);
    }

    //...
}

Apartment a = new Apartment("Soho");
//cast Apartment to House
House h = (House) a;

class MyClass
{
    private int myint;
    public int MyInt
    {
        get { return m_int; }
        set { m_int = value; }
    }
}

MyClass m = new MyClass();
Int i = m.MyInt;
m.MyInt = 23;

//Note: in C# 3.0 the above class declaration is equivalent to:
//(thanks to auto-implemented properties)
class MyClass
{
    public int MyInt { get; set; }
}

//You can also create instantances of these classes with set:
MyClass myClass = new MyClass{ MyInt = 4 };

namespace SampleNamespace
{
    class SampleClass { }

    namespace SampleNamespace.Nested
    {
        class SampleClass2 { }
    }
}

class BaseClass
{
    public void m1() { //... } 
}

class DerivedClass : BaseClass
{
    public new void m1() { //... }
}

DerivedClass derived = new DerivedClass();
derived.m1(); //calls DerivedClass#m1

BaseClass casted = new DerivedClass();
casted.m1(); //calls BaseClass#m1


public class Vector3D
{
    public static Vector3D operator + (Vector3D v) { //.. }
}


class BaseClass
{
    public virtual void m1() { //... } 
}

class DerivedClass : BaseClass
{
    public override void m1() { //... }
}

DerivedClass derived = new DerivedClass();
derived.m1(); //calls DerivedClass#m1

BaseClass casted = new DerivedClass();
casted.m1(); //calls DerivedClass#m1

public int add(params int[] args)
{
    //...
}

//can call #add with variable number of arguments:
add(1);
add(1,2,3); 

class Layout
{
    string[] _values = new string[100]; // Backing store

    public string this[int number]
    {
	get { return _values[number]; }
	set { _values[number] = value; }
    }
}

Layout layout = new Layout();
layout[1] = "Hello world";
String value1 = layout[1];

//Note: in above example, "number" is an int but it can be any type.
//Note: an indexer can use any number of parameters

using (MyResource myRes = new MyResource())
{
    myRes.DoSomething();
}

//is equivalent to

{ // limits scope of myRes
    MyResource myRes= new MyResource();
    try
    {
        myRes.DoSomething();
    }
    finally
    {
        // Check for a null resource.
        if (myRes!= null)
            // Call the object's Dispose method.
            ((IDisposable)myRes).Dispose();
    }
}

//NOTE: object must implement IDisposable interface

The using keyword is also used to import namespaces:

using System;
System.Console.WriteLine("Hello");
Console.WriteLine("World!");

Unsafe Mode Keywords

Unsafe code is for allowing C/C++ style pointer manipulation. See :CSharp:MigratingFromJava2

Resources


No backlinks to this page.
comments powered by Disqus