Jump to content
Nytro

C# by example

Recommended Posts

Posted

Foarte multe exemple pentru diferite actiuni.

Exemple:

using System;
public class HelloWorld
{
public static void Main(string[] args) {
Console.Write("Hello World!");
}
}

using System;
namespace PlayingAround {
class ReadAll {
public static void Main(string[] args) {
string contents = System.IO.File.ReadAllText(@"C:\t1");
Console.Out.WriteLine("contents = " + contents);
}
}
}

public static string getFileAsString(string fileName) {
StreamReader sReader = null;
string contents = null;
try {
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
sReader = new StreamReader(fileStream);
contents = sReader.ReadToEnd();
} finally {
if(sReader != null) {
sReader.Close();
}
}
return contents;
}

using System;
namespace PlayingAround {
class ReadAll {
public static void Main(string[] args) {
string[] lines = System.IO.File.ReadAllLines(@"C:\t1");
Console.Out.WriteLine("contents = " + lines.Length);
Console.In.ReadLine();
}
}
}

StreamReader sr = new StreamReader("fileName.txt");
string line;
while((line= sr.ReadLine()) != null) {
Console.WriteLine("xml template:"+line);
}

if (sr != null)sr.Close(); //should be in a "finally" or "using" block

using System;
namespace PlayingAround {
class ReadAll {
public static void Main(string[] args) {
string myText = "Line1" + Environment.NewLine + "Line2" + Environment.NewLine;
System.IO.File.WriteAllText(@"C:\t2", myText);
}
}
}

using System;
using System.IO;

public class WriteFileStuff {

public static void Main() {
FileStream fs = new FileStream("c:\\tmp\\WriteFileStuff.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
try {
sw.WriteLine("Howdy World.");
} finally {
if(sw != null) { sw.Close(); }
}
}
}

In fine, sunt foarte multe cu sintaxa evidentiata (syntax highlight, pe romaneste):

http://www.fincher.org/tips/Languages/csharp.shtml

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...