Sei sulla pagina 1di 12

C# Programming

C# is a simple, modern, general-purpose, object-oriented programming


language developed by Microsoft

C# is designed for Common Language Infrastructure (CLI), which consists of


the executable code and runtime environment that allows use of various high-
level languages on different computer platforms and architectures.

The following reasons make C# a widely used professional language:

 It is a modern, general-purpose programming language

 It is object oriented.

 It is component oriented.

 It is easy to learn.

 It is a structured language.

 It produces efficient programs.

 It can be compiled on a variety of computer platforms.

 It is a part of .Net Framework.

The .Net Framework


The .Net framework is a revolutionary platform that helps us to write the
following types of applications:

 Windows applications

 Web applications

 Web services

Creating Hello World Program

A C# program consists of the following parts:

 Namespace declaration

 A class
 Class methods

 Class attributes

 A Main method

 Statements and Expressions

 Comments

using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}

Compiling and Executing the Program

If you are using Visual Studio.Net for compiling and executing C# programs,
take the following steps:

 Start Visual Studio.

 On the menu bar, choose File -> New -> Project.

 Choose Visual C# from templates, and then choose Windows.

 Choose Console Application.

 Specify a name for your project and click OK button.


 This creates a new project in Solution Explorer.

 Write code in the Code Editor.

 Click the Run button or press F5 key to execute the project. A Command
Prompt window appears that contains the line Hello World.

1. Program in C# to demonstrate namespaces and aliasing for


namespace classes.
Namespace: A namespace is designed for providing a way to keep one set of
names separate from another.
Example: System.Console.WriteLine();

In this, System is the namespace (scope) in which Console class is located.

A class in a namespace is accessed using the dot operator.

The using Directive is used to import the namespace system.

Defining a Namespace
A namespace definition begins with the keyword namespace followed by the namespace
name as follows

namespace namespace_name
{
// code declarations
}

To call the namespace-enabled version of either function or variable, prepend the namespace
name as follows:

namespace_name.item_name;

Program to demonstrate use of namespaces:

using System;
namespace first_space
{
class namespace_cl
{
public void func()
{
Console.WriteLine("Inside first_space");
}
}
}

namespace second_space
{
class namespace_cl
{
public void func()
{
Console.WriteLine("Inside second_space");
}
}
}

class TestClass
{
static void Main(string[] args)
{
first_space.namespace_cl fc = new first_space.namespace_cl();
second_space.namespace_cl sc = new second_space.namespace_cl();
fc.func();
sc.func();
Console.ReadKey();
}
}

Namespace with using directive

using System;
using first_space;
using second_space;

namespace first_space
{
class abc
{
public void func()
{
Console.WriteLine("Inside first_space");
}
}
}

namespace second_space
{
class efg
{
public void func()
{
Console.WriteLine("Inside second_space");
}
}
}

class TestClass
{
static void Main(string[] args)
{
abc fc = new abc();
efg sc = new efg();
fc.func();
sc.func();
Console.ReadKey();
}
}

Nested Namespaces
You can define one namespace inside another namespace as follows:

namespace namespace_name1
{
// code declarations
namespace namespace_name2
{
// code declarations
}
}

using System;

namespace csharpclass

// nested namespace

namespace sclass

class myExample1

public static void myPrint1()

Console.WriteLine("First Example of calling another namespace member.");

Console.ReadLine();

}
}

// Program start class

class NamespaceCalling

public static void Main()

sclass.myExample1.myPrint1();

sclass.myExample2.myPrint2();

namespace csharpclass.sclass

class myExample2

public static void myPrint2()

Console.WriteLine("Second Example of calling another namespace member.");

Console.ReadLine();

Using Aliases for namespace class


using con = System.Console; // Create an
alias
class MyClient
{
public static void Main()
{
con.WriteLine("Hello! How are you? );
}
}

2. Program in C# to demonstrate escape sequences and verbatim


literal.

C# supports two forms of string literals: regular string literals and verbatim string literals.

Escape Sequence
An escape sequence is a series of special characters which are interpreted by the compiler
as a command. In other words, they suspend the normal processing to perform some special
function.

In C#, escape sequences are represented by a ‘\’ (backslash) followed by a letter or a


combination of digits.

Escape Sequence What it Represents


\’ Single Quotation Mark (character 39)
\” Double Quotation Mark (character 34)
\? Literal Question Mark
\\ Backslash (character 92)
\a Alert/Bell (character 7)
\b Backspace (character 8 )
\f Formfeed (character 12)
\n New Line (character 10)
\r Carriage Return (character 13)
\t Horizontal Tab (character 9)
\v Vertical Tab (character 11)
\ooo ASCII character in octal notation
\xhh ASCII character in hexadecimal notation

using System;

public class DemoEscapeSequence


{

public static void main()

Console.WriteLine(“This line\t contains two\ttabs”);

Console.WriteLine(“This statement\n contains a new line”);

Console.WriteLine(“This statement sounds” + “three alerts\a\a\a”);

verbatim string
A verbatim string literal consists of an @ character followed by a double-quote character,
zero or more characters, and a closing double-quote character. A simple example is @"hello".
In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the
only exception being a quote-escape-sequence.

The @ symbol tells the string constructor to ignore escape characters and line breaks.

String str = @"c:\folder1\file1.txt";


This means that the backslashes are treated literally and not as escape characters.

// Demonstrate verbatim string literals.

Using System;

Class Program

Static void main()

String Name= @”c:\\Program\\Net\\Csharp”;

Console.WriteLine(Name);

}
using System;

class Verbatim {

static void Main() {

Console.WriteLine(@"This is a verbatim

string literal that spans

several lines. ");

Console.WriteLine(@"Here is some tabbed output: 1\t 2\t 3\t 4\t 5 6 7 8 ");

Console.WriteLine(@"Programmers say, ""I like C#.""");

Console.ReadLine();

using System;

class Program

static string _value1 = "String literal";

const string _value2 = "String literal 2";

const string _value3 = "String literal 3\r\nAnother line";

const string _value4 = @"String literal 4

Another line";

const string _value5 = "String literal\ttab";

const string _value6 = @"String literal\ttab";

static void Main()

{
//

// Execution engine begins here.

//

string test1 = "String literal \"1\"";

const string test2 = "String literal 2";

string test3 = @"String literal ""3""";

const string test4 = @"String literal 4";

//

// Print out the string literals.

//

Console.WriteLine(

"{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n{8}\n{9}",

_value1, _value2, _value3, _value4, _value5, _value6,

test1, test2, test3, test4);

Potrebbero piacerti anche