Skip to main content

9 posts tagged with "CSharp"

View All Tags

Abstract Class

· 2 min read

Abstract classes are used for providing an abstraction to the code to make it reusable and extendable.

Abstract class in C++

It is a class that has at least one pure virtual function (i.e., a function that has no definition). The classes inheriting the abstract class must provide a definition for the pure virtual function.

C#: Generate xsd and class for an XML document

· One min read

Using xsd.exe which will be install with the Visual studio can be used.
Open Visual Studio Command Prompt and use the following commands

Setting environment for using Microsoft Visual Studio 2010 x86 tools.

C:Program Files (x86)Microsoft Visual Studio 10.0VC>cd C:

C:>xsd CPPData.xml

Microsoft (R) Xml Schemas/DataTypes support utility [Microsoft (R) .NET Framework, Version 4.0.30319.1] Copyright (C) Microsoft Corporation. All rights reserved. Writing file 'C:CPPData.xsd'. C:>xsd CPPData.xsd /classes Microsoft (R) Xml Schemas/DataTypes support utility [Microsoft (R) .NET Framework, Version 4.0.30319.1] Copyright (C) Microsoft Corporation. All rights reserved. Writing file 'C:CPPData.cs'. C:>

C#: How to Select a random item from a List

· One min read
using System; 
using System.Collections.Generic;
namespace RandomSelect {
class Program {
static List lst = new List();
static void Main(string[] args) {
lst.Add("1");
lst.Add("2");
lst.Add("3");
lst.Add("4");
lst.Add("5");
lst.Add("6");
lst.Add("7");
lst.Add("8");
lst.Add("9");
lst.Add("10");
Random rndElement = new Random();
foreach (string i in lst) {
string s = lst[rndElement.Next(lst.Count)];
Console.WriteLine(s);
}
}
}
}

Part-1: C++, C#, Java Syntax Differences

· One min read

C++ Main Function

  1. Main function with out any arguments
void main() { 
cout<<"Hello World !!!";
}
  1. Main function with commandline Arguments
int main(int argc, char* argv[]) {
cout<<"Hello World !!!";
return 1;
}

C# Main Function

  1. Main function with out any arguments
using System; 
namespace CSharpSample {
class Program {
static void Main() {
Console.WriteLine("Hello World !!!");
}
}
}
  1. Main Function with commandline Arguments
 using System; 
namespace CSharpSample {
class Program {
static int Main(string[] args) {
Console.WriteLine("Hello World !!!");
return 1;
}
}
}

Java Main function

  1. Main function with out any arguments Not possible Compilation error java.lang.NoSuchMethodError: main Exception in thread "main" Main function with commandline Arguments

  2. Main Function with commandline Arguments

public class Program { 
public static void main(String args[]) {
System.out.println( "Hello, World !!!" );
}
}

Part-2: C++, C#, Java Syntax Differences

· 2 min read

[Part-2: Basic Input/Output] C++, C#, Java Syntax Differences

Basic Input/Output

Using the standard input and output library, we will be able to interact with the user by printing messages on the screen and getting the user's input from the keyboard.

1. C++

  • Input    - cin>>
  • Output - cout<<
// Print Greetings Program
// Basic Input/Output
void PrintGreeting(char name[])
{
cout<<"Hello " << name <<endl;
}

int main(int argc, char* argv[])
{
char name[100\];

cout<<"Enter Your Name:"; //cout -> Standard Output
cin>>name; //cin -> Standard Input

PrintGreeting(name);

return 1;
}

/* OUT PUT
Enter Your Name:nagvbt
Hello nagvbt
*/

2. C#

  • Input  - Console.WriteLine()
  • Output - Console.ReadLine()
// Print Greetings Program
// Basic Input/Output
namespace CSharpSample
{
class Program
{
static void PrintGreeting(char[] name)
{
Console.WriteLine("Hello "+ new string(name));
}

static int Main(string[] args)
{
char[] name;

Console.WriteLine("Enter Your Name:"); //Console.WriteLine() -> Standard Output
string sName = Console.ReadLine(); //Console.ReadLine() -> Standard Input
name = sName.ToCharArray();

PrintGreeting(name);

return 1;
}
}
}

/* * OUTPUT * *
Enter Your Name:
Nag VBT
Hello Nag VBT
* * * * * * * */

3. Java

Input - System.out.println()

Output - System.in.read()

import java.io.IOException;

class Program
{
public static void PrintGreeting(char[] name)
{
System.out.println("Hello "+ new String(name));
}

public static void main(String[] args)
{
char[] name;

System.out.print("Enter Your Name:"); //System.out.println -> Standard Output

String sName = "";
int tmp;
try
{
while((tmp = System.in.read ()) != 'n') // System.in.read -> Standard Input
{
char c = (char) tmp;
sName = sName + c;
}

}
catch (IOException e)
{
e.printStackTrace();
}

name = sName.toCharArray();
PrintGreeting(name);
}
}

/* OUTPUT
Enter Your Name:Nag VBT
Hello Nag VBT
*/

Part-3: C++, C#, Java Syntax Differences

· One min read

Comments, Methods, Class, objects Syntax Differences

Comments Same for C++, C#, Java

  1. Single line comments  - //
  2. Multi line comments

/ This is a a multiline comment /

Method/Function declarations

Same, except that in C# and in Java, function must always be part of a class, and must prefix with access specifier - public/private/protected

check main function for reference

Class declarations

Same but c# and Java does not require a semicolon after closing bracket '}'

_ C++ _

class myMath {
//Methods
public: int Add(int i, int j)
{
return i + j;
}
};

_ C#/Java _

 class myMath {
//Methods
public int Add(int i, int j)
{
return i + j;
}
}

Object declaration/creation C++

Object creation on Stack

myMath obj; //on stack
int result = obj.Add(1,2);

Object creation on Heap

myMath \*pobj = new myMath(); //on heap
int result = obj->Add(1,2);
delete pobj;

Java/C#

myMath obj = new myMath();
int result = obj.Add(1,2);

Sending SMS using C

· 2 min read

Step 1: Create a new project in Microsoft Visual Studio 2008 (File -> New -> Project -> Visual C# -> Console Application). Give SMS_Sender name and also specify the location where to store the project.

Step 2: Now add a new item to the project we just created (Project -> Add New Item -> Class). Specify a name to the class as ‘SMSHelper’. The code now looks like as follows. The namespace used is

using System.IO; 
using System.Net;

SMSHelper.cs

 public class SMSHelper { 
private WebResponse myResponse = null;
private string result = string.Empty;
private string formatUrl(string ToMobileNo, string Message) {
DateTime mydate = System.DateTime.Now;
string url = ""; url += "method=sendMessage";
url += "&userid=2000053959"; // your loginId - 1
url += "&password=Gdgek2yiY";//password - 2
url += "&msg=" + mydate.ToString();
url += Message;
url += "&send_to="; // a valid 10 digit phone no.
url += ToMobileNo; url += "&v=1.1";
url += "&msg_type=TEXT"; // Can be "FLASH" or "UNICODE\_TEXT" or "BINARY"
url += "&auth_scheme=PLAIN";
return url;
}

public string SendSms(string ToMobileNo , string Message)
{
try
{
string finalUrl = "http://enterprise.smsgupshup.com/GatewayAPI/rest?" + formatUrl(ToMobileNo, Message);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(finalUrl);
myResponse = myRequest.GetResponse();
Stream st = myResponse.GetResponseStream();

Encoding ec = System.Text.Encoding.GetEncoding("utf-8");
StreamReader reader = new System.IO.StreamReader(st, ec);

result = reader.ReadToEnd();

reader.Close();
st.Close();
return result;
}
catch (Exception exp)
{
return result;
}
finally
{
if (myResponse != null) myResponse.Close();
}
}
}

Step 3: Open Program.cs and in Main write the following lines to send the SMS

 class Program { 
static void Main(string[] args) {
SMSHelper smsObj = new SMSHelper();
string Text = smsObj.SendSms("1234567890", "HelloWorld");
Console.WriteLine(Text);
}
}

Design Pattern: C# Singleton

· One min read
using System;

namespace CSharp {
//Singleton: Ensure a class only has one instance,
//and provide a global point of access to it.
class Singleton {
//Member Variable
private static Singleton instance = null;

//Memeber Functions
private Singleton()
{
}

public static Singleton Instance()
{
if(instance == null )
{
instance = new Singleton ();
}

return instance;
}

public void print()
{
Console.WriteLine("Singleton Class" );
}

};

class Program {

static void Main(string[] args) {
Singleton n = Singleton .Instance();
n.print();
Singleton p = Singleton .Instance();
p.print();
}

}

}

Simple Windows Form in C

· One min read

Minimal code to write a c# windows form:

using System.Windows.Forms;

namespace ConsoleFormApp {
class MyWindow : Form {
public MyWindow() {
this.Text = "First Form";
}
}

class Program {
static void Main(string\[\] args) {
Application.Run(new MyWindow());
}
}
}